LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Shell Script and Cron (https://www.linuxquestions.org/questions/programming-9/bash-shell-script-and-cron-934458/)

metallica1973 03-14-2012 01:55 PM

Bash Shell Script and Cron
 
this is a follow up from a previous post:

http://www.linuxquestions.org/questi...-codes-925648/

I added the entry in crontab that runs the 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 or what am I doing wrong?

sag47 03-14-2012 02:07 PM

You can't run logic like that in cron. The same can be said for piping stdout. I've always gotten around this by creating a third bash script which runs
Code:

/home/testuser/scripts/backups/bkscript.sh full && /home/testuser/scripts/backups/spacechk.sh
and then have cron process the third script.

SAM

T3RM1NVT0R 03-14-2012 02:08 PM

@ Reply
 
Hi metallica1973,

It will be good if you will put them as two seperate entries in cron as follows:

Code:

0 2 * * 6  /home/testuser/scripts/backups/bkscript.sh full
0 2 * * 6  /home/testuser/scripts/backups/spacechk.sh

You can put a time difference accordingly

wpeckham 03-14-2012 03:31 PM

two scripts.
 
@T3RM1NVT0R
If he needs them to run in sequence, the second immediately after the first but never in parallel, your answer is no solution.



I like the idea of the parent script, it gives you some interesting options, like
Code:

#!/bin/bash
if firstscript.sh
then
  secondscript.sh
else
  echo "Dang, first script crapped out on me!"
fi

IE: managing errors and alerting the operator to unexpected behavior.

BUT, I am wondering why a new script could not be written that performs all of the functions of both pre-existing scripts more efficietly, and automatically in the correct order and time.

T3RM1NVT0R 03-14-2012 03:52 PM

@ Reply
 
@ wpeckham,

Thanks for pointing but I did specifically mention that OP can adjust the time difference accordingly. The reason I have suggested to run it that way because if the first one fails then second one will not run at all.

From the name of the script I can understand that the first one is used for backup and the second one is used to check the space left on the device on which backup has been taken. I hope that the OP has configured the script to generate log from where he can see if the scripts ran successfully or not.

Clubbing them as single script is a good idea +1 but it depends on the requirement (organizational policy). If the requirement says that you have to maintain seperate scripts then can't help it.

metallica1973 03-14-2012 04:30 PM

Many thanks

Reuti 03-15-2012 08:50 AM

Quote:

Originally Posted by T3RM1NVT0R (Post 4626845)
The reason I have suggested to run it that way because if the first one fails then second one will not run at all.

Do you want to say, that there is a logic in crontab that later ones are only executed when the ones defined before ran successfully? I’m not aware of such a facility in cron.

metallica1973 03-15-2012 11:57 AM

@wpeckham

So create a child script in which it checks to see if bkscript.sh (@rT3RM1NVT0R correct assumption, these are backup/spacechk scripts) ran successfully and if so then run spacechk or else "Something when wrong". So essentially 3 scripts:

PHP Code:

bkscript.sh 

PHP Code:

spacechk.sh 

and

chkscripts.sh
PHP Code:

#!/bin/bash
if bkscript.sh  
then
echo "bkscript ran successfull so lets proceed"
secondscript.sh
else
   echo 
"Dang, first script crapped out on me!"
fi 

and then add it to cron

PHP Code:

0 2 * * 6   /home/testuser/scripts/backups/bkscript.sh full
0 10 
* * 6   /home/testuser/scripts/backups/chkscripts.sh 

my question is:

How can I tell the chkscript.sh to check the exit status of the bkscript.sh == $?. I want the script to specifically check the exit status of the bkscript.sh and not some other program. Will cron have a issue with the exit status?

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 

??

Reuti 03-15-2012 12:06 PM

You have all there already, but start the check script in cron:
Code:

#!/bin/bash
if bkscript.sh full; then
    echo "bkscript ran successfull so lets proceed"
    spacechk.sh
else
  echo "Dang, first script crapped out on me!"
fi

and only one entry in crontab:
Code:

0 2 * * 6  /home/testuser/scripts/backups/chkscripts.sh

metallica1973 03-15-2012 12:22 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.

wpeckham 03-16-2012 08:50 AM

Reuti had the idea. There is no logic in cron to make events dependent on anything other than time, YOU have to do the work on that.

Unless YOU create a way (which I consider trivial, but I have been doing this since 1969) it is non-trivial for one script to know what another does, or how it exits. All we have discussed are ways to make the shell or a parent process (which CAN read the child process results) do the work for us.

Also, everything here has pre-supposed that the error return would be 0 for success, and anything non-zero would indicate a failure (which would mean the second script should or need not run).

It is MUCH easier to simply run them in order, with no dependencies.
A crontab entry
1 0 * * * scriptone ; scripttwo
would work for that.

If your first script is sending results codes with more meaning than simple success and failure, someone needs to know what those are and for what values the second script should run. This is a slightly less trivial problem, and SHOULD take some thought. You also need to think about what results you have now, what you NEED, and what script and scheduling changes will get where you want to go most reliably.

I suggest digesting both scripts, thinking over coffee, and creating a new script that does everything you REALLY need done, in the correct order, and without needless complications.

Then you have just one script to schedule: simple and easy.

metallica1973 03-16-2012 04:20 PM

sounds like a plan, many thanks

metallica1973 03-19-2012 03:32 PM

Quote:

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


/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.
Gentleman,

I finished my testing and in fact the change that I made to my exit status of my script fixed it. So crontab supports this logic "&&". I did not need to create a third script.


All times are GMT -5. The time now is 03:14 AM.