Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Executing script in normal/daemon mode and adding start/stop capability

by almut (Canon)
on Oct 07, 2009 at 12:35 UTC ( #799705=note: print w/replies, xml ) Need Help??


in reply to Executing script in normal/daemon mode and adding start/stop capability

* 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.)

  • Comment on Re: Executing script in normal/daemon mode and adding start/stop capability

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://799705]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2016-10-01 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (9 votes). Check out past polls.