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:
|
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 SAM |
@ 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 |
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 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. |
@ 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. |
Many thanks
|
Quote:
|
@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:
PHP Code:
chkscripts.sh PHP Code:
PHP Code:
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:
PHP Code:
|
You have all there already, but start the check script in cron:
Code:
#!/bin/bash Code:
0 2 * * 6 /home/testuser/scripts/backups/chkscripts.sh |
I think I may know why all of this did not work from the begginning using cron:
PHP Code:
|
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. |
sounds like a plan, many thanks
|
Quote:
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. |