Copy and Modify the Example Init Script

Start by copying /etc/init.d/skeleton to /etc/init.d/mouselogger, and marking it executable:

cd /etc/init.d
sudo cp skeleton mouselogger
sudo chmod +x mouselogger

We need the sudo here because (like most things in /etc) files in /etc/init.d are owned by root. (You can imagine what might happen on a system with lots of users if just anyone could muck around in the startup files.)

Next, open this up in Nano:

nano mouselogger

I had to change three things. First, the metadata between BEGIN INIT INFO and END INIT INFO to specifically reference a service called "mouselogger" and provide a description:

Second, the environment variables that the rest of the script uses to do its business. I added /home/pi to the path, changed the description to "Mouse logger.", and changed the name to, as well as emptying the DAEMON_ARGS variable:

And lastly, I added --background and --make-pidfile to the start-stop-daemon options in the do_start() function:

This ensures that the script is sent to the background instead of running in the foreground after it starts, and creates a file in /var/run that contains the process ID of the script.

Test the Script and Install with update-rc.d

You should now be able to run /etc/init.d/mouselogger - but first, make sure that /home/pi/ isn't already running.  If you have it open in a terminal, you can type Ctrl-c to kill the process. Otherwise, use ps aux | grep to see if it has a running process:

Here, I checked with ps and found a copy of the Python interpreter running, so I killed the corresponding process with sudo kill 2052. (2052 is the process id in the second column of the output from ps.)

Now you can try starting and stopping the service and testing it out:

sudo /etc/init.d/mouselogger start
ps aux | grep
curl localhost
sudo /etc/init.d/mouselogger stop

Once you're confident that the init script works, you can install it in the default runlevels so that it starts at boot time:

sudo update-rc.d mouselogger defaults


And now, the moment of truth! Reboot and see what happens...

sudo reboot

This part might take a bit, but once you can log back in, it should be simple to test whether is running:


Next up, we'll look at doing the equivalent thing with systemd on a BeagleBone Black.

This guide was first published on Sep 01, 2015. It was last updated on Sep 01, 2015.

This page (SysV Init: Writing an Init Script for was last updated on May 17, 2021.

Text editor powered by tinymce.