How to Use Nohup to Run Process as a Background Process in Linux

How to use nohup on a process already running in background

nohup starts a new process. You cannot retroactively apply it to a process that you've already started.

However, if the shell from which you launched the job is bash, ksh, or zsh then the disown job-control builtin may provide what you want. It can either remove the job from job control altogether or just flag the job to not be sent a SIGHUP when the parent shell itself receives one. This is similar, but not necessarily identical, to the effect of starting a process via the nohup command.

Note well that your job may still have issues if any of its standard streams is connected to the session's terminal. That's something that nohup typically clobbers preemptively, but disown cannot modify after the fact. You're normally better off anticipating this need and starting the process with nohup, but if you're not so foresightful then disown is probably your next best bet.

Note also that as a job-control command, disown takes a jobspec to identify the job to operate on, not a process ID. If necessary, you can use the jobs builtin to help determine the appropriate jobspec.

Run process/script in background using nohup

Adding another "&" did it for me:

echo Begin!
ssh -i file root@IP 'nohup ./zookeeper-3.4.6/bin/zkCli.sh -server IP >/dev/null & '
echo Zookeeper connected
sleep 15
ssh -i file root@IP 'nohup ./apache-storm-0.9.3/bin/storm nimbus &' &
echo Nimbus started
sleep 15
ssh -i file root@IP 'nohup ./apache-storm-0.9.3/bin/storm ui &' &
echo UI started
sleep 15
ssh -i file root@IP 'nohup ./apache-storm-0.9.3/bin/storm supervisor &' &
echo End!

how can i run a linux process in the background and log the nohup output

Try

nohup bash -c "./manage.py celeryd -B -E -l DEBUG > ../../logs/celeryd_beat.nohup 2> ../../logs/celeryd_beat.err" &

How do I put an already-running process under nohup?

Using the Job Control of bash to send the process into the background:

  1. Ctrl+Z to stop (pause) the program and get back to the shell.
  2. bg to run it in the background.
  3. disown -h [job-spec] where [job-spec] is the job number (like %1 for the first running job; find about your number with the jobs command) so that the job isn't killed when the terminal closes.

How to bring a nohup task to foreground after the shell is closed

Use screen instead of nohup:

screen -dmS demo bash -c 'while ! read -t 1;do echo $((i++));done'

Note: there are no &.

Then exit... Later, you could:

screen -x demo

Hit keys Ctrl+a, then d to leave console running

Or

gnome-terminal -e 'screen -x demo'

Then, simply close window to leave process running.

How to get the process ID to kill a nohup process?

When using nohup and you put the task in the background, the background operator (&) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via kill PID or kill -9 PID (if you need to force kill). Alternatively, you can find the PID later on by ps -ef | grep "command name" and locate the PID from there. Note that nohup keyword/command itself does not appear in the ps output for the command in question.

If you use a script, you could do something like this in the script:

nohup my_command > my.log 2>&1 &
echo $! > save_pid.txt

This will run my_command saving all output into my.log (in a script, $! represents the PID of the last process executed). The 2 is the file descriptor for standard error (stderr) and 2>&1 tells the shell to route standard error output to the standard output (file descriptor 1). It requires &1 so that the shell knows it's a file descriptor in that context instead of just a file named 1. The 2>&1 is needed to capture any error messages that normally are written to standard error into our my.log file (which is coming from standard output). See I/O Redirection for more details on handling I/O redirection with the shell.

If the command sends output on a regular basis, you can check the output occasionally with tail my.log, or if you want to follow it "live" you can use tail -f my.log. Finally, if you need to kill the process, you can do it via:

kill -9 `cat save_pid.txt`
rm save_pid.txt

Find the Process run by nohup command

ps auxwww|grep -i 'server' should return all process which has server in them. Otherwise, server may have already stopped.

You should be able to determine the PID (and store it in a file) as follows:

nohup server &
print $! >> my_server.pid


Related Topics



Leave a reply



Submit