upstart init.d systemd - a simple instruction


  • systemd is now supported by most linux system, and is the recommended way contrast to old upstart and init
  • the systemd config file is under /lib/systemd/system
  • env vars only able to be set in ExecStart or ExecStartPre, best way is to set in the script
  • WorkingDirectory can not use double quote, seems to be a bug?
  • if you app run like java -jar, should use Type=simple instead of Type=forking, otherwise it will case a timeout

Make a service auto-start

after move the config under the folder /lib/systemd/system

sudo systemctl enable servicename.service

sudo systemctl daemon-reload

Run/Stop Service

sudo systemctl start servicename.service

sudo systemctl stop servicename.service

View log

#view status, little log
systemctl status servicename.service 

#view the last 50 lines
systemctl status service-name -n50  

#view all the log
systemctl --no-pager -l status servicename.service 

#view all the log
journalctl -u service-name.service 

#view the last 50 lines
journalctl -n 50 --no-pager -u service-name.service 

#tail the log
journalctl -f -u service-name.service 


Type=simple #for java, should not be forking
#note the bash should specify its full path
ExecStart=/bin/bash /home/ubuntu/




upstart is designed to replace init, but then
upstart is replaced by systemd from ubuntu 16

  • place servicename.conf under /etc/init/ which is system job.
  • place servicename.conf under $HOME/.init/ which is user job.
  • console log is written to /var/log/upstart/servicename.log
  • it support log rotating

Run/Stop Service

sudo start servicename

sudo stop servicename


author ""
description "start and stop example for Ubuntu (upstart)"
version "1.0"

start on started networking
stop on runlevel [!2345]

setuid ubuntu
setgid ubuntu

# set env
env APPUSER="ubuntu"
env APPDIR="/usr/bin"


	cd $APPDIR	#use env
    #set env via shell script
	JAVA_OPTS="\ \ \
	-Djava.rmi.server.hostname=localhost \ \ \
	exec /usr/bin/java $JAVA_OPTS -jar jetty-runner.jar \
	    --classes "." \
	    --host  --port 8080 \
end script


The Story Behind ‘init’ and ‘systemd’: Why ‘init’ Needed to be Replaced with ‘systemd’ in Linux

Total views.

© 2013 - 2023. All rights reserved.

Powered by Hydejack v6.6.1