Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to spawn a persistent netcat server. My command of choice is the following:

echo "bash -c \"while [ 1 ]; do nc -l -p 1111 >> check; done\"" | at now

I am wondering how I can get the PID of the process created by at so that I can easily put the server to sleep when needed.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

This is a tad tricky because of quoting, note change from " to '

The following will work if you submit your at job via at -f file

at -f nc.on now
cat nc.on
bash -c 'while [ 1 ]; do echo $$ > /var/run/atnc.pid; nc -l -p 1111  >> check; done'

the file /var/run/atnc.pid will have the process id of the bash which is running nc

You can cat the file to get the bash process id and kill it, terminating nc. Then rm /var/run/atnc.pid (optional).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.