Created
November 4, 2015 11:16
-
-
Save marshyon/c49c9ecb1ffcbaaaa085 to your computer and use it in GitHub Desktop.
Ubuntu Upstart service plus simple Perl script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
description "sleepy server" | |
start on runlevel [2345] | |
stop on runlevel [!2345] | |
# set up an environment variable | |
env TIME=inf | |
# restart this process if it dies | |
respawn | |
# disable console | |
console none | |
# do any pre start stuff to set up for process to run ok | |
pre-start script | |
#test -x /usr/sbin/sshd || { stop; exit 0; } | |
#test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; } | |
#mkdir -p -m0755 /var/run/sshd | |
end script | |
exec /usr/local/bin/sleeping_app.pl $TIME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use File::Pid; | |
my $pidfile = "/var/run/sleeping_app.pid"; | |
$SIG{TERM} = sub { unlink($pidfile); exit; }; | |
$SIG{INT} = sub { unlink($pidfile); exit; }; | |
die "there is already a $0 process running\n" if ( -e $pidfile ); | |
my $pf = File::Pid->new({ | |
file => $pidfile | |
}); | |
$pf->write; | |
while(1) { | |
sleep 10; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a trivial Perl script that can be managed on an Ubuntu or Ubuntu like system with 'upstart' :
http://upstart.ubuntu.com/wiki/Documentation/WritingAService
to see if the service is running, after creating the /etc/iniit/sleep file,
the 'service' can be stopped / started with
If the process is killed, respawn will restart the process. Here the pid '7614', from the above ps command is killed,
and immediately after the process is running again
The sleeping Perl app does nothing, it just 'sleeps' but ofcourse can be modified, adding things that need to be done before or after the 'sleep' command, for example :
The section in the upstart 'sleep' file under 'pre-start script' is commented out but left for reference. It is where things can be done to check if the script to be run exists, if directories are present etc. It is left for this to be edited as appropriate or to be deleted if not.