Alex Chen Alex Chen - 1 year ago 140
Linux Question

Getting sudo and nohup to work together

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 ./ 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 ./ 20070502 ctm_20070502.csv &
[2] 19603
[~user]$ nohup: appending output to `nohup.out'
ls format_wrds_trd.txt nohup.out norm_wrds_trd.cfg
[2]+ Stopped nohup sudo ./ 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.

Answer Source

The problem here, imho, is not nohup, but background processing sudo.

You are putting the process in background (& at end of command) but probably sudo needs password authentication, and that is why the process stops.

Try one of these:

1) remove the ampersand from end of command, reply to passord prompt and afterwords put it in background (by typing CTRL-Z - which stops the process and issuing the bg command to send it to background)

2) Change the /etc/sudoers to not ask for users password by including the line: myusername ALL=(ALL) NOPASSWD: ALL

If besides the password reply your application waits for other input, then you can pipe the input to the command like this: $ cat responses.txt|sudo mycommand.php


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download