10

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
  • 6,676
  • 24
  • 76
  • 106

5 Answers5

22

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,513
  • 1
  • 19
  • 27
  • *"stop it"* ... *"restart the job"*... do you mean "pause it" and "resume the job" ? or really *stop* the job and start **from the beginning** again? – ADTC Dec 11 '15 at 04:48
  • 1
    @ADTC: well "stop" is the terminology used by the operating system itself (CTRL+Z sends a TSTP signal), but pause/resume would be more understandable... I'll update the answer :) – Joril Dec 11 '15 at 11:37
  • ctr-z would have killed an scp job. – alvas Jun 28 '16 at 10:18
  • 1
    I'm going to comment here that jobid IS NOT THE SAME AS process ID (which is what I naturally thought) - see https://superuser.com/questions/276748/list-job-ids-instead-process-ids. also, you *need* to include the % symbol in your commands. – nealio82 Apr 26 '17 at 09:55
  • This is all very fine to keep the job running on disconnect, but I find that TSTP (^Z) of a parent script will suspend the job that was disown'ed. Subsequent ^C will then let the job continue, but is there any way to say "don't take any signals from the terminal" ? (perhaps a different question...) And, yes, I have redirected stdin/stdout/stderr to /dev/null – user67327 Jun 27 '21 at 01:02
14

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,151
  • 2
  • 41
  • 71
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
  • 1
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,803
  • 10
  • 9
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,380
  • 1
  • 17
  • 15