Shell – Execution order with multiple commands

cronshell

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?

Best Answer

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.


As suggested by mikeserv, for testing in general, you can use true (or :) and false, instead of ls of a non-existent file. Hence,

$ false && echo ok1 ; echo ok2
ok2

$ false && (echo ok1 ; echo ok2)
Related Question