Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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

share|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'... –  coincoin Dec 5 '12 at 16:00

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.

share|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

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' &
share|improve this answer

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 &

share|improve this answer
    
It still waits until both commands are complete before returning to the command prompt. –  Jakobud 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... –  Jakobud 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/… –  Iain Aug 24 '12 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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