Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm relatively new to Linux and I'm working on a CronTab for scheduled backups. I figured out how to do multiple sequential jobs with ; and &&, but I'm unsure how the dependency affects the sequence.

My cron job looks like this:

# Every day at 0:00 (reboot server and do the hourly backup procedure).
0 0 * * * sh shutdown.sh && sh backup.sh ; sh nas.sh ; sh ftp.sh ; sh startup.sh        

What I want to happen is to run shutdown.sh and continue the sequence if it's successful, but cancel it if it fails. My fear is it will only skip sh backup.sh but then continue the sequence.

Will it work as intended? If not, would something like shutdown && (backup ; nas ; ftp ; startup) be possible?

share|improve this question
    
It will work as written so long as you exit 1 (or anything other than 0) from shutdown.sh in the event of a failure. –  mikeserv yesterday
2  
The whole thing should be a script, so that it's easy to debug and test. Your cron then just call that 1 script (which can then contain "if" statements, etc) –  Olivier Dulac 20 hours ago
    
That is a good idea, I will do that. This was mostly a working document to visualize my intentions easier. –  Taesh 17 hours ago
add comment

2 Answers

up vote 8 down vote accepted

Why don't you test it with some dummy commands that you know will work or fail?

$ ls foo && echo ok1 ; echo ok2
ls: cannot access foo: No such file or directory
ok2

$ ls foo && (echo ok1 ; echo ok2)
ls: cannot access foo: No such file or directory

So it seems like your intuition was correct, and you need the second structure.

share|improve this answer
    
Of course, I should have done that. The reason I asked is that I'm not familiar with command formatting. But thank you! –  Taesh yesterday
    
No worries. It's still a good question (+1). I tend not to trust any answers immediately anyway, and prefer ones that I can test myself. –  Sparhawk yesterday
add comment

if shutdown.sh really reboots your server, i don't think the other commands will get executed?? answer might be deleted, but you can read it, so here are just some hints: place the scripts in an own folder and call them with full path, cron might run in a different folder. making them executable and calling them directly without sh is nice too. putting the whole logic in an extra script with an if-else is good coding too. if you worry about sh being used as shell, use a shebang.

share|improve this answer
    
This is for a server instance running on a Debian box. –  Taesh yesterday
    
as long as you know what you do it's fine then :) –  justaquestion yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.