7

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).

  • 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 7:45
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`).

| improve this answer | |
  • 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 8: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'... – user130370 Dec 5 '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
3

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 &

| improve this answer | |
  • 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 php.net/manual/en/function.exec.php and it references stackoverflow.com/questions/3748432/… – user9517 Aug 24 '12 at 17:28
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' &
| improve this answer | |
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.

| improve this answer | |
  • screen -dmS would be better, then. – 3molo Aug 24 '12 at 18:03
  • Maybe but I think easier to understand showing things separated: open screen, give the commands and detach than everything on the same line, having to give a name to the session. – laurent Aug 25 '12 at 13:52
  • screen is great for interactive use, but is not appropriate for scripting or automation. – Jonathan Hartley Jun 13 '16 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.