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
Kill with nohup process ID
You don't need nohup
. It does exactly two things:
- Any of stdin, stdout or stderr that are attached to the TTY get redirected (to
/dev/null
in the stdin case, or tonohup.out
in the stdout and stderr cases). - HUP signals received by nohup are not propagated to the child.
The first is just the same as normal shell redirection (but with a hardcoded destination, which you normally don't want anyhow). The second achieves the same goal as using disown -h
to tell the shell not to propagate a HUP to the targeted child (which is default behavior in the first place when your shell is noninteractive).
Thus, you can avoid needing nohup
entirely, with something like:
yourcommand </dev/null >/dev/null 2>&1 & yourcommand_pid=$!
disown -h "$yourcommand_pid"
...which lets you later run kill "$yourcommand_pid"
.
How to kill a nohup process?
kill -0
does not kill the process. It just checks if you could send a signal to it.
Simply kill pid
, and if that doesn't work, try kill -9 pid
.
Process ID of nohup process continually updating - cant kill process
Very very bad question ...
You are trying to kill you
grep
process...ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
The command is grep --color=auto nohup
- I'm not sure you can kill nohup
nohup
will run your command in a particular way. But after its launching, the nohup
process dies.
How to kill a NOHUP process launched via SSH
Log in again, use ps -a to find the relevant process IDs, then kill it with kill.
Related Topics
Mount Smb/Cifs Share Within a Docker Container
How to Display Number to Two Decimal Places in Bash Function
How to Use Variables in Bash Sed Command, Specific Example
How to Extract Duration Time from Ffmpeg Output
Looping Through the Content of a File in Bash
Assembling 32-Bit Binaries on a 64-Bit System (Gnu Toolchain)
Why Should Eval Be Avoided in Bash, and What Should I Use Instead
Is There a Way For Non-Root Processes to Bind to "Privileged" Ports on Linux
What Registers Are Preserved Through a Linux X86-64 Function Call
How to Replace a String in Multiple Files in Linux Command Line
How to Obtain the Number of Cpus/Cores in Linux from the Command Line
Controlling a Usb Power Supply (On/Off) With Linux
How to Rename All Folders and Files to Lowercase on Linux
How to View the List of Functions a Linux Shared Library Is Exporting
How to Loop Over Directories in Linux
Error When Using Git Credential Helper With Gnome-Keyring as Sudo