0

I want to make a delayed background execution, for delay I use sleep [anyseconds] && [execution] for background I use nohup

Simple example of nohup alone:

nohup date &>> out.log &

And of course you can find print out of the execution of date in out.log.

But I want to delay the execution, like 15 seconds, so a little combination:

nohup sleep 15 && date &>> out.log &

This time it does not work properly, sleep 15 got executed while the date will not always do - If you shut down the terminal, it will not execute anything after the &&.

What is wrong with nohup? It surrenders to &&??

George Y
  • 380
  • 2
  • 11

1 Answers1

3

I assume you are in bash, where nohup is a command of the system (coreutils).

In bash the && mark the end of a commandline. In your case the nohup gets from the shell only the parameters sleep 15. All the rest is the next command for the shell.

As nohup is not a shell, you can not use parenthesis to "extend" the parameters to nohup, because nohup can not interpret it and does not know what to do with it.

One solution would be to create a script (e.g. sleepdate) which contains sleep 15 && date and use this as parameter to nohup:

nohup ./sleepdate &>> out.log &

or use the bash (which can interpret &&) as parameter to nohup

nohup bash -c 'sleep 15 && date' &>> out.log &
Marco
  • 316
  • 1
  • 6