9

I need to nohup two commands, one after another. The second command has to be executed AFTER the first command. Here are the commands:

tar -zcf archive.tar.gz.tmp mydir
mv archive.tar.gz.tmp archive.tar.gz

I need the commands to run in the background, but obviously not both at the same time. The second command has to be run after the first command is complete. I tried this:

nohup tar -zcf archive.tar.gz.tmp mydir ; mv archive.tar.gz.tmp archive.tar.gz > /dev/null 2>&1 &

but the commands don't run in the background. The bash prompt waits until the two commands are complete before giving me another prompt. I need them to run in the background.

If I do:

nohup tar -zcf archive.tar.gz.tmp mydir > /dev/null 2>&1 &

That runs in the background perfectly. It's only when I try to do more than one command.

I also tried:

nohup tar -zcf archive.tar.gz.tmp mydir > /dev/null 2>&1 & ; mv archive.tar.gz.tmp archive.tar.gz > /dev/null 2>&1 &

But that didn't work either. Invalid syntax or something. I'm sure there is a way to do this but I'm just not sure the syntax.

The only alternative I can think of is to put the commands into a .sh file and nohup that file, but that isn't necessarily something I can do on the linux system I have to implement this on (privileges limitations etc).

Jake Wilson
  • 8,494
  • 29
  • 94
  • 121
  • I know this is an old question. Independent of the chosen nohup solution you should join your two commands with `&&` instead of `;` so the second command only is executed if the first succeeded (`&&` is a specific bash command). – Martin Hennings Sep 12 '16 at 07:45

4 Answers4

4

I think you need parentheses.

nohup $(tar -zcf archive.tar.gz.tmp mydir; mv archive.tar.gz.tmp archive.tar.gz) >/dev/null 2>&1 &

Aaron Copley
  • 12,345
  • 5
  • 46
  • 67
  • It still waits until both commands are complete before returning to the command prompt. – Jake Wilson Aug 24 '12 at 17:05
  • What's the shell? I get a prompt immediately in Bash. Final `&` backgrounds the whole job. – Aaron Copley Aug 24 '12 at 17:07
  • Hit enter. You get a prompt. – Aaron Copley Aug 24 '12 at 17:10
  • Ah yeah okay it is running correct at the bash shell. I tried running it via PHP's `exec()` command but it waits for the second command to run. Strangely if I do just one command it behaves as expected though... – Jake Wilson Aug 24 '12 at 17:14
  • @Jakobud: The example of nohup php exec() is in the php manual http://php.net/manual/en/function.exec.php and it references http://stackoverflow.com/questions/3748432/insane-crond-behavior-keeps-making-defunct-bash-processes/3750028#3750028 – user9517 Aug 24 '12 at 17:28
  • @Iain that solution worked perfectly in PHP. Thanks! – Jake Wilson Aug 24 '12 at 17:50
4

Easiest solution: Create a shell script with

 #/bin/sh
 tar -zcf archive.tar.gz.tmp mydir
 mv archive.tar.gz.tmp archive.tar.gz

And use nohup myscript. Possibly with some extra documentation in the script.

(Yes, I am a sucker for documentation. Especially when working with multiple people or when you might someday leave the job and your successor needs to figure out why".)


An alternative might be to start a new shell for a subtask (in this case your tar and move)

nohup `$(tar -zcf archive.tar.gz.tmp mydir; mv archive.tar.gz.tmp archive.tar.gz)


Alternative 2:

nohup ```tar -zcf archive.tar.gz.tmp mydir ; mv archive.tar.gz.tmp archive.tar.gz```.

I really prefer the script though. The comments in it might be quite useful when you or someone else looks at he script in a few years and wonders why it was done this way.

On a unrelated note: How the heck do I post an anser with a backtick in it? (The command for a shell to 'use this command ; feed the output to another command as input`).

Hennes
  • 4,772
  • 1
  • 18
  • 29
  • Well, good practice dictates that you should use `$()` rather than back-ticks, anyway. But then that would be identical to my answer. – Aaron Copley Aug 24 '12 at 17:12
  • actually backticks will try to evaluate the result of the expression inside -, so depending on the commands, this may not work. – Michael Nov 13 '12 at 08:53
  • alternative 2 does not seem to work : mkdir testdir touch testdir/testfile then run nohup `cd ; cd testdir; ls` return failed to run command `testfile'... –  Dec 05 '12 at 16:00
  • "succor" means a much needed help, such as comforting someone who is distressed, and doesn't seem entirely appropriate here. Possibly you meant the common English phrase "I am a sucker for X" . – Jonathan Hartley Jun 13 '16 at 18:47
2

Maybe a bash wrap?

nohup bash -c 'tar -zcf archive.tar.gz.tmp mydir > /dev/null 2>&1 ; mv archive.tar.gz.tmp archive.tar.gz > /dev/null 2>&1' &
ghm1014
  • 944
  • 1
  • 5
  • 14
0

You can use screen to give the commands and detach from the screen session:

screen
tar -zcf archive.tar.gz.tmp mydir && mv archive.tar.gz.tmp archive.tar.gz
ctrl-a d to detach

The commands will run as with nohup.

laurent
  • 2,035
  • 16
  • 13