Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Solved] No redirect if script ran with cron

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
csabix
Posts: 194
Joined: 2007-09-23 21:15
Location: Sf Gheorghe
Contact:

[Solved] No redirect if script ran with cron

#1 Post by csabix »

Hello all


I have a small script that looks like this:

#!/bin/bash
ls -l
echo "end of listing"
echo 0 > this_script.sh

Basically, what it does is execute two commands and after it, it has to erase its own contents. I won't go into details regarding the reason I want this to happen because it's a long story (but might make a tutorial on this forum in case it works).
If I execute

Code: Select all

sh this_script.sh
, it runs well and at the end, the file will contain only a zero character, as it is supposed to do.
This script must be executed with cron on a regular basis. Problem is, the last line (echo 0 > this_script.sh) will not get executed. Anything else will. I also double checked this reading the user's mail sent by cron: it displays the outputs proving that the script is executed.

So what is wrong with that redirect?
Last edited by csabix on 2014-08-26 11:06, edited 1 time in total.

csabix
Posts: 194
Joined: 2007-09-23 21:15
Location: Sf Gheorghe
Contact:

Re: No redirect if script ran with cron

#2 Post by csabix »

* * * * * sh /home/raiduser/test/autoerase.sh

User avatar
saulgoode
Posts: 1445
Joined: 2007-10-22 11:34
Been thanked: 4 times

Re: No redirect if script ran with cron

#3 Post by saulgoode »

If the cron job is run as user raiduser then the last line will try to remove the file /home/raiduser/this_script.sh (by default cron jobs execute in the user's home directory).
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian Kernighan

csabix
Posts: 194
Joined: 2007-09-23 21:15
Location: Sf Gheorghe
Contact:

Re: No redirect if script ran with cron

#4 Post by csabix »

It doesn't remove the file. It writes into it.
I also tried redirect of a command to a different file, so the script remains intact. It doesn't work either.
AND same user can run script successfully from command line.

User avatar
buzzin
Posts: 121
Joined: 2013-12-31 09:42

Re: No redirect if script ran with cron

#5 Post by buzzin »

Have you tried quotes around the 0?
It looks to me like it echo's the entire line....
Sysadmin/KDE user

csabix
Posts: 194
Joined: 2007-09-23 21:15
Location: Sf Gheorghe
Contact:

Re: No redirect if script ran with cron

#6 Post by csabix »

Quoting doesn't help either.
I checked the mails sent by the system and I see cron tries to run it in /home/raiduser. Somehow it doesn't want to descend into test directory. When I moved the script to the user's homedir, it worked.
Checked the path in crontab many times and still cannot see what is wrong. I also gave 777 permissions to the directory and the script.

Later edit:
Managed to make it work after inserting a line into the script that says: "cd /home/raiduser/test".
It's so crazy!

PS: I want to replace cron with a daemon-like something for 2 reasons:
1. cron sends mail every minute
2. cron executes only once a minute
There's need for something to watch the script and execute it if the content is not a zero.
Is there a simple way?

csabix
Posts: 194
Joined: 2007-09-23 21:15
Location: Sf Gheorghe
Contact:

Re: No redirect if script ran with cron

#7 Post by csabix »

Thanks, wizard10000!
Those indeed are nice alternatives.
The hard part was figuring out the path issue. I have other scripts on my server and all are running well. I never realized the need for this path stuff due to the fact that most scripts were written to start with cd /path/to/dir. When I made them years ago, it seemed natural to do it that way. :))

Now, for the daemon-sort stuff. I googled around and saw more ways to do it:
- there's the /etc//init.d/skeleton way. It seems a bit complicated because I didn't find a tutorial that explains how configuring works. I need to access some detail info.
- the script.sh& way (run in background). This seems very simple but it won't start after rebooting the server.
- start-stop-daemon - this I don't really udnerstand
- "setsid myscript.sh >/dev/null 2>&1 < /dev/null &" way

Do you people have experience with stuff like this and which would you recommend for me?
The only thing that script has to do is monitor the autoerase.sh file and if it does not contain a zero, than run autoerase.sh script.

Post Reply