Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting sudo and nohup to work together

Tags:

linux

sudo

nohup

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

like image 439
Alex Chen Avatar asked Jul 04 '13 16:07

Alex Chen


People also ask

What is the difference between nohup and &?

nohup catches the hangup signal (see man 7 signal ) while the ampersand doesn't (except the shell is confgured that way or doesn't send SIGHUP at all). Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal ( kill -SIGHUP <pid> ).

Can I run multiple nohup commands?

You can run multiple commands in the background by using nohup command. In the following command, mkdir and ls command are executed in the background by using nohup and bash commands. You can get the output of the commands by checking output.

Can I close the terminal with nohup?

Using 'nohup' With Your Command Nohup will separate the process from the terminal. Because of the disconnect, it will not receive SIGHUP, which is why it is named nohup. If you close the terminal, the process will keep running until you close it.


2 Answers

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

like image 78
dusktreader Avatar answered Sep 24 '22 08:09

dusktreader


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

hth

like image 27
Bandiera Avatar answered Sep 23 '22 08:09

Bandiera