Getting sudo and nohup to work together

Alex Chen picture Alex Chen · Jul 4, 2013 · Viewed 46k times · Source

Linux newbie here.

I have a perl script which takes two command line inputs. I tried to run it in the background but this is what I got:

[~user]$ nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
[2] 19603
[~user]$ nohup: appending output to `nohup.out'

after the system returns "nohup: appending output to `nohup.out'", no new prompt will appear. Then as long as I type in some other command, the shell will tell me that the process is stopped:

[~user]$ nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
[2] 19603
[~user]$ nohup: appending output to `nohup.out'
ls
ascii_loader_script.pl  format_wrds_trd.txt  nohup.out  norm_wrds_trd.cfg
[2]+  Stopped                 nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv

I've looked at this post and tried to do "sudo date" before executing the command. Still got the same thing. http://www.sudo.ws/pipermail/sudo-users/2003-July/001648.html

Answer

dusktreader picture dusktreader · Nov 13, 2013

The solution is to use the -b flag for sudo to run the command in the background:

$ sudo -b ./ascii_loader_script.pl 20070502 ctm_20070502.csv

You should only use nohup if you want the program to continue even after you close your current terminal session