Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

Your Answer


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.