I am trying to start tcpdump as a background process, and get its PID. The script will monitor the system for disk space, cpu and memory, and kill the tcpdump process if any goes beyond the threshold. But I cant get the PID using $!. The output is blank.
#!/bin/bash
`nohup tcpdump port 389 -i bond0 -s 0 -w /tmp/capture.pcap > nohup2.out 2>&1&`
my_pid=$!
echo $my_pid
flag=1
exit 1
and the output is
bash # sh t.sh
bash #
Change this:
`nohup tcpdump port 389 -i bond0 -s 0 -w /tmp/capture.pcap > nohup2.out 2>&1&`
to this:
nohup tcpdump port 389 -i bond0 -s 0 -w /tmp/capture.pcap > nohup2.out 2>&1 &
The backticks mean that the command should be run in a subshell and its output captured and substituted into a new command. For example, this command:
`echo foo bar`
is equivalent to this command:
foo bar
because it runs echo foo bar
, captures the output foo bar
, and then runs foo bar
. In your case you have absolutely no need of that (if only because you're redirecting the output to a file), so you can just drop them.