* How can I add start/stop commands for the script.
One way to do it would be to add the respective options to your script
(as you're already using Getopt::Long, this should be rather
straightforward). The implementation would typically involve a
pid-file to store the PID which you need to communicate with a potentially running instance
of the script. Something like:
-
start
- Check if there's a pid-file.
- If so, read the PID from the file, and send that PID signal number
zero to check if it's still alive.
- If it is alive, output "... already running", and quit.
- In case no running instance is found, start the daemon the same
way you would do from the command line. When successful, create a
pid-file containing the PID of the process.
-
stop
- Check if there's a pid-file.
- If there is no pid-file, output "... doesn't seem to be running",
and quit.
- Otherwise, read the PID from the file, and send that PID the TERM
signal (or whatever signal makes your script quit gracefully).
- Delete the pid-file. (In case you're paranoid, you might want to
wait a while and then check whether the TERM signal did actually
terminate the process, before deleting the pid-file. If not, output an
error message.)