46

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

Alex Chen
  • 535
  • 1
  • 5
  • 7
  • 3
    May I suggest another solution? It is likely that you want to use `nohup` to protect the process from closing the terminal. If yes, you might be better off with `screen`. – January Jul 04 '13 at 16:58
  • suggested workaround: add `sudo echo hi` or something else to your `.bashrc` file, so you entered the password once right at the beginning – phil294 Mar 07 '16 at 17:28

10 Answers10

57

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

dusktreader
  • 3,845
  • 7
  • 30
  • 40
  • 16
    Thank you very much. Now I can do wonderfull things like `nohup sudo -b mongod`. – Tad Lispy Mar 14 '14 at 13:29
  • 3
    Wait but what if you do want the process to continue even after you close your current terminal session? – Kurt Wheeler Apr 19 '19 at 21:03
  • I would like to follow up on what @KurtWheeler said, what happens if you close your terminal? And what happens to output from the command? That is the whole point of nohup, that it is in the background, output goes to a file, and it doesn't stop when you quit the terminal. Documentation on the sudo -b options leaves a lot to be desired. – noisygecko Aug 06 '20 at 22:52
23

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

Bandiera
  • 377
  • 2
  • 5
9

You can Try

sudo su

and then

nohup ./ascii_loader_script.pl 20070502 ctm_20070502.csv &

instead of

nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
ThomasDr
  • 176
  • 1
  • 7
  • 3
    I think this is better than both the accepted answer and the highest voted answer. Neither of those actually address how to get nohup running with sudo. – Kurt Wheeler Sep 29 '20 at 19:58
  • This one served me the best! – Pedro Queiroga Aug 13 '21 at 11:45
  • My boss keeps doing this and it messes up so much stuff. He mainly uses Windows and just dabbles in Linux. Edit: because it changes your user rather than executes commands using your user with elevated privileges. – grofte Nov 23 '22 at 12:09
5

You must use sudo first, nohup second.

sudo nohup ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
QkiZ
  • 798
  • 1
  • 8
  • 19
  • 6
    For other readers, the command line shown here is not correct but the comment about order is. The ampersand will background sudo here rather than the command being run as sudo; use the -b flag to sudo instead like other answers mention. – pydsigner Jan 28 '16 at 05:45
  • 1
    Why?? What is the reason of using 'sudo' first? – Mohsen Abasi Dec 02 '17 at 11:20
  • Bumping the question above - please explain why this order is correct. – flow2k Nov 16 '20 at 21:44
  • From that, I remember when I used nohup first it didn't work. I don't remember what was a reason. – QkiZ Nov 18 '20 at 10:45
2

My working solution for evaluating disk fragmentation in the background:

  1. Exec sudo with nohup without ampersand (&) at the end:
  $ sudo nohup nice -20 find / -type f -exec filefrag "{}" \; | sed 's/^\(.*\): \([0-9]\+\) extent.*/\2\t\1/'| awk -F ' ' '$1 > 0' | sort -n -r | head -50 > filefrag.txt  
  1. Enter password for sudo;
  2. Press Ctrl+Z;
  3. Put the running process in the background.
$ bg 1
[1]+ sudo nohup nice -20 find / -type f -exec filefrag "{}" \; | sed 's/^\(.*\): \([0-9]\+\) extent.*/\2\t\1/' | awk -F ' ' '$1 > 0' | sort -n -r | head -50 > filefrag.txt &
  1. Now you can exit the terminal and log in later. The process will remain running in the background. Because nohup is used.
Eronex
  • 21
  • 1
1

This should work

sudo -b -u userName ./myScript > logFile

I am just curious to understand that can I send this logFile as a email after the ./myScript is successful running in background.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Utsav
  • 5,572
  • 2
  • 29
  • 43
1

First of all, you should switch sudo and nohup. And then:

if sudo echo Starting ...
then
    sudo nohup <yourProcess> &
fi

The echo Starting ... can be replaced by any command that does not do much. I only use it as dummy command for the sudo.

By this the sudo in the if-condition triggers the password-check. If it is ok then the sudo session is logged in and the second call will succeed, otherwise the if will fail and not execute the actual command.

Frederic Leitenberger
  • 1,949
  • 24
  • 32
1

I open an editor and typed these lines:

#!/bin/bash
sudo echo Starting ...
sudo -b MyProcess

(Where MyProcess is anything I want to run as superuser.)

Then I save the file where I want it as MyShellScript.sh .

Then change the file permissions to allow execution. Then run it in a terminal. the "-b" option tells sudo to run the process separately in the background, so the process keeps running after the terminal session dies.

Worked for me in linux-mint.

DVOPS
  • 36
  • 4
1

You can set it as your alias:

sudo sh -c 'nohup openvpn /etc/openvpn/client.ovpn 2>&1 > /dev/null &'
DarkBee
  • 16,592
  • 6
  • 46
  • 58
0

Try:

xterm -e "sudo -b nohup php -S localhost:80 -t /media/malcolm/Workspace/sites &>/dev/null"

When you close xterm, the PHP web server still alive.
Don't put nohup before sudo or else the PHP web server will be killed after closing xterm.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
diyism
  • 12,477
  • 5
  • 46
  • 46