Score:0

`nohup` does not work properly with `&&`

vn flag

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 &&??

Score:3
in flag

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 &
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.