Bash Shell Script and Cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Shell Script and Cron
# 1  
Old 03-14-2012
Bash Shell Script and Cron

How can I tell a script to only execute if the other scripts exits successfully?

So "script A" executes and if it executes successfully (0),then "script B" will run

or else

"script A "executes and it exits unsucessfully (1) then "script B" will read return code (1) and will not execute

I have an entry in crontab that run two shell scripts:

PHP Code:
0 2 * * 6   /home/testuser/scripts/backups/bkscript.sh full && /home/testuser/scripts/backups/spacechk.sh 
the bkscript.sh scripts runs fine (exit code 0)and executes successfully but it will not run the second spacechk.sh script. I can run spacechk.sh manually and all works as designed. It like it not reading the &&, is there a better way to do it?
# 2  
Old 03-14-2012
That should work IF
bkscript.sh returns 0(zero) and only zero.

Chances are that your environment is broken in the cron, and the first script barfs some where.

Do a dummy run like this, just one time use the same user's crontab, then delete it.
test.sh with execute permissions:
crontab -e this entry into cron
Code:
#!/bin/[whatever shell you use goes here]
set > /tmp/set.$$

Code:
* * * * *  /path/to/test.sh

Give it a minute ior two.
crontab -e and remove the test.sh.

Now compare what you have in the /tmp/set[n] file with the expected environment.
# 3  
Old 03-14-2012
Firstly does cron have your environment, a quick check is to source in the local profile for your user.

There are many ways of doing this we'd normally use a token file or the like.

Normally I would put a token in the last line of the log file or in a separate file, then check the token with the following script. You must remember to either create a new logfile or null the logfile at the begining of the first script and you should test that it has done so.

In the first file something like;

Code:
print "Completed Ok" >> ${logfile}

In the following script;

Code:
if
          tail -1 ${logfile} | grep "Completed Ok" | grep -v grep
                then
                      Do Your Bits Here!
                else
                      print "The Backup had a problem - please investigate." >> ${logfile}
fi

This is just one of many ways you could do this, this very simplistic and probably wouldn't be allowed in a production environment. Ensure that any tests that you write are suitable for purpose - key to any of them is testing the result and failing safe!

Regards

Dave
# 4  
Old 03-15-2012
Awesome. Many Thanks

---------- Post updated at 01:25 PM ---------- Previous update was at 01:02 PM ----------

I think I may know why all of this did not work from the begginning using cron:

PHP Code:
/home/testuser/scripts/backups/bkscript.sh full && /home/testuser/scripts/backups/spacechk.sh 
After taking a look at my bkscript.sh, I had many exit 1 and exit 0 statuses in various places. When the Full or Incremental section of the script is run, I am using an exit 1 , so when bkscript.sh was finished, it had an "1" exit status, so echo $? == 1. Therefore the "&&" would not execute spacechk.sh because of the exit status error within my script was a "1". I have not tested it but I am pretty sure that is what the orginal issue was.

---------- Post updated at 01:32 PM ---------- Previous update was at 01:25 PM ----------

In response to you previous post:

So what you suggested

I could add an entry to a log file:

PHP Code:
#!/bin/bash
if bkscript.sh  
then
echo "bkscript ran successfull so lets proceed"
secondscript.sh
print "Completed Ok" >> ${logfile}
exit 
0
else
   echo 
"Dang, first script crapped out on me!"
exit 1
fi 
In the following script;
PHP Code:
if
          
tail -${logfile} | grep "Completed Ok" grep -v grep
                then
                      
Do Your Bits Here!
                else
                      print 
"The Backup had a problem - please investigate." >> ${logfile}
fi 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

4. Shell Programming and Scripting

how do I run bash script using cron job

How do I run bash script using a cron job? I have tried to just write the path of the script, but that didn't work. (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

changing cron using bash script

How can I change the cron entries only for ABC and XYZ from dosomething_1.0.sh to nowchanged_2.0 using a bash script ? Any help will be appreciated. # # ABC 00,05,10,15,20,25,30,35,40,45,50,55 * * * * /mydir/dosomething_1.0.sh 1>/dev/null 2>&1 # # ## # DEF... (4 Replies)
Discussion started by: jville
4 Replies

6. Shell Programming and Scripting

Shell script and Cron

Hi there, I have a script that pulls in a weather feed via FTP then writes the output to a file. This script is run everyday at 3pm by cron. Heres the shell script: #!/bin/sh HOST='ftp.weatheronline.co.uk' USER='my username' PASSWD='my password'... (2 Replies)
Discussion started by: mpk_3
2 Replies

7. Shell Programming and Scripting

Cron job shell script..

Hey Guys, i was trying out a shell script which has to remove a file for every 90 mins. this is the code i came up with . $ crontab -e file1 file1 contains 30 1 * * * * rm -r /folder1/folder2/somefile.txt Now i need the cron to run for every 90 mins. the problem with this is... (8 Replies)
Discussion started by: Irishboy24
8 Replies

8. Shell Programming and Scripting

General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?

Status quo is, within a web application, which is coded completely in php (not by me, I dont know php), I have to fill out several fields, and execute it manually by clicking the "go" button in my browser, several times a day. Thats because: The script itself pulls data (textfiles) from a... (3 Replies)
Discussion started by: lowmaster
3 Replies

9. AIX

Is it possible to have more than one Cron in shell script?

Hi, can we have more than one cron in the shell script? If so pls let me know on what basis it is running. Whether on OS scheduling or the time scheduled in the cron file.Pls answer this query. Many Thanks (4 Replies)
Discussion started by: Yamini Thoppen
4 Replies

10. HP-UX

executing shell script from the cron

This isn't the usual problem that a shell script runs from the command line and not the cron. It's a little different. Among other things, the shell scrip executes my .profile to set a bunch of variables. It then does an env to ensure that it ran OK. There are echos in the shell script and... (2 Replies)
Discussion started by: abNORMal
2 Replies
Login or Register to Ask a Question