Ubuntu: How to Start Program as Service

Ubuntu: How To Start Program As Service
by admin admin Date: 10-10-2013 ubuntu service server

According to Ubuntu’s Upstart website, “Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.” Basically, Upstart is an init daemon that replaces the /etc/init.d scripts. It comes with Ubuntu 6.10 and later, and it is also used in Fedora and Debian.

If you want to run your program with Upstart, you have to write a job for it. Jobs are defined in files placed in /etc/init with the .conf extension. The job will be named after the file it is defined in (without the .conf extension). The job definitions are written in plain text files (should not be executable).


description     "Logs keypresses, screenshots, clipboard data, and browsing history to a database."

# Start when gnome starts
start on started gdm
stop on shutdown

# Set any Environmental variables you need
env DISPLAY=:0.0

exec /usr/local/greeenlog/main.pyw

After saving this job, you can go ahead and run it. No reboot is necessary. Here are the commands (assuming you have a job named greeenlog):

$ status greeenlog
greeenlog stop/waiting

status returns the last action performed on the job (start or stop) followed by the current state (running, sleeping, waiting, etc.) and the process ID (if any).

$ start greeenlog
start: Rejected send message, 1 matched rules; type="method_call", sender=":1.61" (uid=1000 pid=2496 comm="start) interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply=0 destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init"))

The start command tells Upstart to start the job, but what was that garbage I got for a response? Well, after getting some help, I realized that all I needed to do was add sudo:

$ sudo start greeenlog
greeenlog start/running, process 2943

You can also stop the service with the stop command.

I used the job defined above to run my personal keylogger as a service. However, It often would fail to start properly at boot. Running the service manually later with sudo start greeenlog would start the keylogger correctly, but what kind of shitty keylogger needs to be started manually? After two days of tinkering, I’ve learned that Upstart is not the right answer for startup applications that depend on GNOME or X, even if they do require root access. If want to run an application at boot that requires root access, you should try Run Application as Root at Startup instead.

You might notice that I defined an environment variable in my Upstart job. Upstart only provides baseline environment variables. Here are the variables that were provided to my job by default, captured in a Python dictionary with os.environ:

{'TERM': 'linux', 'PWD': '/', 'UPSTART_INSTANCE': '', 'UPSTART_JOB': 'greeenlog', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'}
by admin admin Date: 10-10-2013 ubuntu service server hits : 6380  

Related Posts