LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Issues while executing shell scripts through Cron job (http://www.linuxquestions.org/questions/programming-9/issues-while-executing-shell-scripts-through-cron-job-654293/)

paragkalra 07-08-2008 08:59 AM

Issues while executing shell scripts through Cron job
 
First Script: one.sh
cat /data/backup/scripting/shell-scripts/cron-script/one.sh

Quote:

#!/bin/sh
var=three
sh /data/backup/scripting/shell-scripts/cron-script/two.sh $var
Second Script: two.sh
cat /data/backup/scripting/shell-scripts/cron-script/two.sh

Quote:

#!/bin/sh
echo $1
touch $1.sh
When I manually run "one.sh" it successfully executes also executing "two.sh" but when I execute "one.sh" using cronjob it doesn't executes.

#cronjob -l
* * * * * /data/backup/scripting/shell-scripts/cron-script/one.sh

colucix 07-08-2008 09:11 AM

Is one.sh executable? How do you tell the script has not been executed? Take in mind that the command
Code:

touch $1.sh
creates the file in the HOME directory of the user who launched the crontab, since you did not specify a full path. Also check the user's mail: he would have received a mail from the cron daemon, containing the output of the echo command, since you did not redirected the standard output to a file.

jomen 07-08-2008 09:12 AM

Doesn't it work if you make the two scripts executable and then just use them by their name (without "sh" in front...)?
Why would you do it the way you described?

paragkalra 07-08-2008 09:35 AM

Yes both the files are executable.

I have changed the two files:

cat /data/backup/scripting/shell-scripts/cron-script/one.sh
Quote:

#!/bin/sh
var=three
/data/backup/scripting/shell-scripts/cron-script/two.sh $var

cat /data/backup/scripting/shell-scripts/cron-script/two.sh
Quote:

#!/bin/sh
echo $1
touch /data/backup/scripting/shell-scripts/cron-script/$1.sh
When I am manually executing "one.sh" i.e when I am executing following command:
# /data/backup/scripting/shell-scripts/cron-script/one.sh

then "three" is being echoed & "three.sh" is also getting created in "/data/backup/scripting/shell-scripts/cron-script/"

colucix 07-08-2008 09:40 AM

Is the cron daemon running? Did you receive any mail from the cron daemon?

chrism01 07-08-2008 07:37 PM

Yep, check your email and or root's, depending on whose crontab this is.
However, just to point out that jobs in cron are not attached to a terminal, so 'echo' is a pointless cmd, unless you re-direct it to a file eg
echo "$var1" >/dir/somefile
Given that the default env/PATH var is minimal in cron, you should always specify the full path eg
/bin/touch /path/file

paragkalra 07-10-2008 01:09 PM

Thanks 2 all for your prompt replies........I got it resolved......It was a permission related issue....

tbarnes24 07-31-2008 12:53 PM

I'm having the same problem with FreeBSD
 
What was the permissions issue, and how did you resolve it? I've got a pair of scripts that I'm trying to run via the root cron, but they don't execute, even though I updated the cron normally.

paragkalra 08-02-2008 01:18 PM

The path variable that is set under cronjob is different from the one that is set under normal circumstances i.e cronjob path variable is different from the one "#env | grep -i path"

So execute the shell script by specifying full path name of the commands or explicitly export the path variable.


All times are GMT -5. The time now is 08:48 AM.