Reputation: 1129
So I have a problem similar to how to send ssh job to background.
I have a windows c# program automated to execute tcpdump on a remote linux os using http://sshnet.codeplex.com/. I'm trying to execute tcpdump on the remote linux and leave it running after I disconnect.
I've been doing a lot of debugging using plink, but cannot seem to achieve the desired result. I've tried:
plink [email protected] bash -c "tcpdump -i eth0 -w test.cap"
but it holds the sshclient until I ctrl+C (not going to work for automated solution). I've also tried variations of:
plink [email protected] bash -c "tcpdump -i eth0 -w test.cap &"
but either the command is not executed at all (test.cap does not exist) or is terminated immediately (test.cap contains 1 line). During testing, I've left a ping going, so the capture should have somthing...
The previously mentioned link solves the problem with screen, but the remote linux os is not configurable and does not have screen. Any suggestions are welcome.
Upvotes: 4
Views: 7759
Reputation: 504
I had a similar situation: (on windows machine) i wanted to create a ms batch script to open an SSH connection to a raspberry pi and execute a local script in the background. I found that combining both Raj's and fahd's answers did the trick for me:
my ms batch script:
plink -load "raspberry Pi" -t -m startCommand.txt
the content of startCommand.txt is as follows:
nohup /home/pi/myscript >/dev/null 2>&1 &
w
exit
The ">/dev/null 2>&1 " is important! I found out (the hard way) that the RPi's SDcard kept getting full by an extremely large nohup.out file (and with a full SDcard, the RPi couldn't even login properly)
reasoning:
I used the -load to load a saved session in PuTTY (i do this because i am authenticating with public/private keys instead of passwords, but this should be the same as simply typing in the host)
then -t (as recommended by Raj)
then -m to load a list of commands in that file
without the parameter "-t" and without the "w" and "exit", my batch script would just run, not execute 'myscript' and close again.
Upvotes: 1
Reputation: 23
I think that I've nailed it, at least in IBM AIX
I'm using
ssh -tq user@host "/path/start-tcpdump.ksh"
(authentication is done by publick key). I was having inconsistent results using simple "nohup tcpdump .... &", sometimes it worked, sometimes it did not, sometimes it even blocked and I had to disconnect the session. So far, this is working ok, I can't really say WHY it is working, but it is...
This is my start-tcpip.ksh
#!/usr/bin/ksh
HOST=$(uname -n)
FILTER="port not 22"
(tcpdump -i en1 -w $HOST-en1.cap $FILTER >/dev/null 2>&1 ) &
sleep 2
(tcpdump -i en2 -w $HOST-en2.cap $FILTER >/dev/null 2>&1 ) &
sleep 2
exit 0
Upvotes: 0
Reputation: 1784
I had the same problem, and I found that the "-t" option seems to be important to nohup. I found the nohup wasn't taking affect without the "-t" option.
ssh -t user@remote 'nohup tcpdump -i any -w /tmp/somefile &>/dev/null & sleep 2'
Upvotes: 0
Reputation: 6186
I had the same issue. I had a scrip in which I had nohup tcpdump .... & . I could not use ssh to run it as it dies when the ssh finished. The solution I came up with was super simple. I just added sleep 5 to the end of my script and it works just fine. It seems tcpdump needs some seconds to go to background safely before you exit even with nohup.
Upvotes: 0
Reputation: 36
I had a similar problem while starting a remote application. This pattern worked for me on Debian servers:
ssh root@server "nohup /usr/local/bin/app -c cfg &; exit"
addition: for another test the above didn't work, ie. the command didn't start on the remote server. Adding a command that returns successfully before the exit seems to work.
ssh root@server "nohup /usr/local/bin/otherapp &; w; exit"
Upvotes: 2
Reputation: 3330
In the latter case, your tcpdump
process is probably being aborted when you disconnect. Try:
plink [email protected] bash -c "nohup tcpdump -i eth0 -w test.cap &"
See the manpage for nohup
. You may also want to consider redirecting stdout and stderr to a file or /dev/null
to prevent nohup
from writing output to a file:
plink [email protected] bash -c "nohup tcpdump -i eth0 -w test.cap >/dev/null 2>&1 &"
Upvotes: 6