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
How to get a list of programs running with nohup
When I started with $ nohup storm dev-zookeper
,
METHOD1 : using jobs
,
prayagupd@prayagupd:/home/vmfest# jobs -l
[1]+ 11129 Running nohup ~/bin/storm/bin/storm dev-zookeeper &
NOTE: jobs
shows nohup processes only on the same terminal session where nohup
was started. If you close the terminal session or try on new session it won't show the nohup
processes. Prefer METHOD2
METHOD2 : using ps
command.
$ ps xw
PID TTY STAT TIME COMMAND
1031 tty1 Ss+ 0:00 /sbin/getty -8 38400 tty1
10582 ? S 0:01 [kworker/0:0]
10826 ? Sl 0:18 java -server -Dstorm.options= -Dstorm.home=/root/bin/storm -Djava.library.path=/usr/local/lib:/opt/local/lib:/usr/lib -Dsto
10853 ? Ss 0:00 sshd: vmfest [priv]
TTY column with ?
=> nohup
running programs.
Description
- TTY column = the terminal associated with the process
- STAT column = state of a process
- S = interruptible sleep (waiting for an event to complete)
- l = is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
Reference
$ man ps
# then search /PROCESS STATE CODES
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
how to find whether a script run as a nohup finished or not?
At the beginning of your shell script, write the PID to a file (for example, in /var/run). Then, you can just search for that PID to know if the process is done or not. You can get the PID of your shell script using the built-in $$
variable.
To record the PID, put at the top of your script:
echo $$ > /var/run/myscript.pid
Then, to check if it's still running:
ps -p `cat /var/run/myscript.pid`
You might not be able to write into /var/run
as a normal user. If not, just use /tmp
How can I see the output console after running a nohup command?
You can redirect standard output and standard error to a file and look at that file. eg:
nohup command 2>&1 > outputfile &
note default behavior from man page:
If standard output is a terminal, append output to 'nohup.out' if
possible, '$HOME/nohup.out' otherwise. If standard error is a
terminal, redirect it to standard output
so really you can just run
nohup command &
and then look in nohup.out
Get PID of nohup process through ssh
Don't use (command &) &
. Just use (command) &
(or just command &
). With (command &) &
, Bash needs to fork another sub-shell to run the command so $!
would be the PID of the sub-shell other than the command.
See following example:
[STEP 124] # (nohup sleep 1234 > /dev/null 2>&1 &) &
[1] 44420 <=== this is the PID of the sub-shell which died immediately
[STEP 125] #
[1]+ Done ( nohup sleep 1234 > /dev/null 2>&1 & )
[STEP 126] # ps p 44420 <=== as you can see this PID does not exist any more
PID TTY STAT TIME COMMAND
[STEP 127] # ps -C sleep u
USER PID TTY STAT START TIME COMMAND
root 44421 pts/13 S 17:48 0:00 sleep 1234
[STEP 128] # kill 44421
[STEP 129] #
This would work fine:
[STEP 131] # (nohup sleep 1234 > /dev/null 2>&1) &
[1] 44424 <=== this is the PID of the sleep command
[STEP 132] # ps p 44424 u
USER PID TTY STAT START TIME COMMAND
root 44424 pts/13 S 17:49 0:00 sleep 1234
[STEP 133] #
Job -l after nohup
You need to understand the difference between a process and a job. Jobs are managed by the shell, so when you end your terminal session and start a new one, you are now in a new instance of Bash with its own jobs table. You can't access jobs from the original shell but as the other answers have noted, you can still find and manipulate the processes that were started. For example:
$ nohup sleep 60 &
[1] 27767
# Our job is in the jobs table
$ jobs
[1]+ Running nohup sleep 60 &
# And this is the process we started
$ ps -p 27767
PID TTY TIME CMD
27767 pts/1 00:00:00 sleep
$ exit # and start a new session
# Now jobs returns nothing because the jobs table is empty
$ jobs
# But our process is still alive and kicking...
$ ps -p 27767
PID TTY TIME CMD
27767 pts/1 00:00:00 sleep
# Until we decide to kill it
$ kill 27767
# Now the process is gone
$ ps -p 27767
PID TTY TIME CMD
how to fined a process started with nohup after closing terminal?
One way to distinguish between these processes are through there stdout
redirections and there is no good way of doing that using ps
command.
If you have pgrep
installed, you can use that with a simple for loop
to know which pid
correspond to which output file. Something like the following,
for pid in $(pgrep julia);
do
echo -n "$pid: ";
readlink -f /proc/${pid}/fd/1;
done
/proc/${pid}/fd/1
represents the stdout
for the process with pid
. It's a symlink, so you need to use readlink
to check the source.
Output:
12345: /path/to/output1.txt
12349: /path/to/output2.txt
12350: /path/to/output3.txt
Alternative way would be to use lsof -p $pid
, but I find it a bit on the heavy side than what you want to achieve, but the output would be same.
for pid in $(pgrep julia);
do
lsof -p $pid | awk -v var=$pid '/1w/ {print var": "$9}';
done
Running process nohup
You are using nohup
right from what I know. But you have an issue detecting the process.
jobs -l
only give the processes of current session. Rather try the below to display the process started in your initial session:
ps -eafww|grep run_terasort.sh|grep -v grep
Related Topics
Add Column to End of CSV File Using 'Awk' in Bash Script
Shell Script Working Fine Without Shebang Line? Why
How to Identify Multiple Usb-Serial Adapters Under Ubuntu 10.1
Error Running Make: Missing Separator (Did You Mean Tab Instead of 8 Spaces)
Any Way to Specify the Location of Profile Data
Elf Dynamic Loader Symbol Lookup Ordering
Interprocess Communication via File
Dma Cache Coherence Management
Get the Characters After the Last Index of a Substring from a String
How to Know Whether a Copy-On-Write Page Is an Actual Copy
Configure and Build Opencv to Custom Ffmpeg Install
Interrupt Handling and User Space Notification
Can't Install Freetds via Yum Package Manager