1

How could I concatenate several commands in a single line, but also run them all in the background ? These didn't work:

sleep 1m & && echo "goodbye" & && exit &.
sleep 1m & ; echo "goodbye" & ; exit &.
sleep 1m & echo "goodbye" & exit &.

I must have all commands to effect foreground session. For example, exit & should end foreground session.

Note: I use it as an alternative to the at utility. The story of why I need this alternative is long, but it can be told from these questions I asked here (and presented chronologically below), to I sadly didn't have a satisfying solution (regarding my specific case):

Here, the answer by dataved seems promising.

3 Answers3

1

How about

pid=$$; ( sleep 1; echo goodbye; kill $pid ) & echo hello
dataved
  • 79
  • Can you combine nohup with that ?... –  Dec 29 '16 at 12:47
  • You are welcome to answer here: http://unix.stackexchange.com/questions/333538/how-could-i-add-nohup-to-this-command –  Dec 29 '16 at 13:26
1

Well, IMO, the best way to do that and to spare the trouble would be to use the screen utility:

1. screen -R <name-of-the-screen-session>
2. <the-command(s)> 
3. ctrl + a + d ### To exit the screen session
4. To log back: screen -x <name-of-the-screen-session>
VVelev
  • 222
0

The && is a conditional operator. It will execute the command to the right if-and-only-if the command on the left exited successfully. Since you're executing the command to the left in the background, its return code cannot be determined and things break and don't work as expected.

Don't use &&. You should either do command1 & command2 & command3 or ( command1; command2; command3 )&. The second one will execute all the commands sequentially in a subshell that's running in the background.

Robin479
  • 335