Supose I have a script "script1.sh" and I want to execute it as a daemon with nohup and &, and make it be "waiting" for a signal. How should I do that?

  • Why nohup? you won't be able to send SIGHUP to the process after that. – geirha May 11 '15 at 5:18
  • Strictly speaking, there's no way for a child process to become a daemon directly from an interactive login session. The system is designed to prevent child processes from being able to "escape" being tracked as part of the login session. You're using Ubuntu Linux. If you really want a daemon, look at systemd's "user services" for version 15. Look at upstart's "session jobs" for prior versions. If you just want a background process then don't conflate that with being a daemon. – JdeBP May 11 '15 at 7:17

In bash, the latest backround process' PID is stored in $!:

nohup <command> & pid=$!

To send a process a signal you can use kill:

kill -<signal> <pid>

Putting the pieces toghether, to start e.g. a background watch -n1 echo foo process and send it e.g. a SIGTERM signal later:

nohup watch -n1 echo foo & pid=$!
# ...
kill -15 $pid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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