3

I wrote a Perl script which will serve as a daemon, so I'd like to let the OS (in my case, Ubuntu Linux) automatically run my Perl script after it has booted up.

How can I do that?

3

Make a shell script wrapper for your perl script

#!/bin/sh - script.sh
# your perl program goes here

/bin/perl /path/to/foobar.pl

Make sure that you have given executable permission using

  chmod +x script.sh

and execute the following,

sudo update-rc.d script.sh defaults

This will run perl script on each startup.

0

Things have changed a bit in the last 7 years. In Ubuntu 16.04, the path to perl is different. Also, you are required to supply a bunch of configuration information in your /etc/init.d/foobar.sh script:

#!/bin/sh foobar.sh
### BEGIN INIT INFO
# Provides:          foobar
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: forbarsvr
# Description:       foobar daemon for widget serving
### END INIT INFO
sudo -u foobarusr perl /path/to/foobar.pl &

This form of invoking perl allows you to run as a user (giving a constrained security domain) rather than root; to run as root, remove the -u foobarusr. If you are executing as a non-root user, ensure that user has permissions over all required resources, such as the perl script itself.

The trailing & fires off your perl script as an on-going (until finishing) task; deamon-like if it doesn't terminate for whatever reason.

Make your foobar launcher executable:

sudo chmod +x /etc/init.d/foobar.sh

Add your script into the startup sequence:

sudo update-rc.d foobar.sh defaults

Note there is no path supplied to the script.

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.