Sign up ×
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 && sh ; sh ; sh ; sh        

What I want to happen is to run and continue the sequence if it's successful, but cancel it if it fails. My fear is it will only skip 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 in the event of a failure. – mikeserv Jun 15 '14 at 0:16
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 Jun 15 '14 at 8:05
That is a good idea, I will do that. This was mostly a working document to visualize my intentions easier. – Taesh Jun 15 '14 at 10:31

2 Answers 2

up vote 10 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

$ 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

$ false && (echo ok1 ; echo ok2)
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 Jun 15 '14 at 0:19
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 Jun 15 '14 at 1:21
@Sparhawk - I like that philosophy - it's one I subscribe to as well. You might find it a little easier to do in future with : and false though. – mikeserv Jun 16 '14 at 5:53
@mikeserv Paranoia is good. :) I did think about using true and false, and almost edited it in, but I thought ls of an non-existent file was clearer, since you could see an error message, and it was more "real world". It's still good advice though, so I'll edit the answer. – Sparhawk Jun 16 '14 at 5:56
@Sparhawk - the output was a good point. Its no better really, but maybe handy when you want to fail and echo ... >&2 in the same call: (${unset?...}) || executed – mikeserv Jun 16 '14 at 7:48

if 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 Jun 15 '14 at 1:56
as long as you know what you do it's fine then :) – justaquestion Jun 15 '14 at 2:00

Your Answer


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.