12

On a linux system is there any way to use nohup when the process that is being nohuped required input, such as an rsync command that needs a password to be entered but will then run happily on its own?

DrStalker
  • 7,266

6 Answers6

27

If the command doesn't have to be scripted, you can do it this way:

  1. run it in the foreground
  2. pause it (CTRL+Z)
  3. disown it so that it won't be closed when you close your shell (disown -h %jobid)
  4. resume the job in the background (bg %jobid)
Joril
  • 1,680
16

You want to look at screen. Screen will create a shell session for you, which you can detach and then reattach at a later date. Try:

 # screen rsync -a directory server@directory

You can type in your password, verify that it's running as you expect and then press 'ctrl-a' followed by 'd'. You should now be detached from your screen session. If you want to see how it's getting on, run

 # screen -r

and you should be reattached. 'ctrl-a' 'd' will detach you again.

When the command finishes, screen should quit.

David Pashley
  • 23,963
2

The (very late) answer is:

Yes, you can use nohup with a process that requires input:

A: You can either start your process as you would usually:

  • So you type nohup ... & like so:

    nohup rsync -av john@doe.io:/from/here/ ./to/here &
    

    Now when you hit enter the process will start but is detached so you are in the situation you encountered and previewed as a problem: How to enter the credentials for user john at doe.io?

  • Type jobs in your terminal and you will see something like:

    [1]+  Stopped                 nohup rsync -av john@doe.io::/from/here/ ./to/here
    

    The [1]+ indicates the jobid (here it is just 1). Also, the process in now actually halted since the rsync command is waiting for input, hence the Stopped state.

  • Type fg %<jobid> so in the example it would be fg %1 (don't forget the %, it's part of the syntax!)

  • Now you brought the process to the foreground and can interact with it.

B: Or simply ditch the ampersand, so the process stays in the foreground (and you essentially follow the the accepted answer by @Joril).

  • So you would type:

    nohup rsync -av john@doe.io:/from/here/ ./to/here
    

    And you readily can interact with the process.


In both cases, A or B, now you can interact with the process:

  • In our example the last line of in your terminal would now read something like:

    john@doe.io's password: 
    
  • Once you are done interacting hit Ctrl + Z which will halt the process

  • Type jobs and you will see something like:

    [1]+  Stopped                 nohup rsync -av john@doe.io::/from/here/ ./to/here
    
  • The only thing now left to do is to make the process running again. To do so simply type: bg %<jobid> (so bg %1 in our example).

  • We are done! Go grab a coffee and and check your nohup.out file from time to time to see if the process has finished.

j-i-l
  • 121
2

Some modules on the remote daemon may require authentication. If so, you will receive a password prompt when you connect. You can avoid the password prompt by setting the environment variable RSYNC_PASSWORD to the password you want to use or using the --password-file option. This may be useful when scripting rsync.

found this in the man page

dude
  • 21
2

If possible it might be better for the specific instance of running an rsync command to set up SSH key authentication without a password rather than try to automatically stuff a password into rsync.

mtinberg
  • 1,863
1

Aside from what's been said above, the most general solution might be to wrap the command in a script that takes input from a file and passes it on (perhaps using pipes, or expect, or something like that). The wrapper script itself is then just a normal command that doesn't require input, of course.

Lee B
  • 3,490