Skip to content

Instantly share code, notes, and snippets.

@zbypy
Created November 22, 2013 15:25
Show Gist options
  • Save zbypy/7601666 to your computer and use it in GitHub Desktop.
Save zbypy/7601666 to your computer and use it in GitHub Desktop.
How to create linux service
The whole source of our daemon service is below. To use it for your own purposes do the following:
replace the name vsftpdg in the third line with your program name,
replace /usr/local/vsftpdg/pid with the folder where you’ll be running your program,
replace “nohup java -jar /usr/local/vsftpdg/vsftpdg_server.jar” with the path to your own program,
you’re good to go!
#!/bin/sh
### BEGIN INIT INFO
# Provides: vsftpdg
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/stop vsftpdg server
### END INIT INFO
case $1 in
start)
echo "Starting vsftpdg ..."
if [ ! -f /usr/local/vsftpdg/pid ]; then
nohup java -jar /usr/local/vsftpdg/vsftpdg_server.jar /usr/local/vsftpdg 2>> /dev/null >> /dev/null &
echo $! > /usr/local/vsftpdg/pid
echo "Vsftpdg started ..."
else
echo "Vsftpdg is already running ..."
fi
;;
stop)
if [ -f /usr/local/vsftpdg/pid ]; then
PID=$(cat /usr/local/vsftpdg/pid);
echo "Stopping vsftpdg ..."
kill $PID;
echo "Vsftpdg stopped ..."
rm /usr/local/vsftpdg/pid
else
echo "Vsftpdg is not running ..."
fi
;;
restart)
if [ -f /usr/local/vsftpdg/pid ]; then
PID=$(cat /usr/local/vsftpdg/pid);
echo "Stopping vsftpdg ...";
kill $PID;
echo "Vsftpdg stopped ...";
rm /usr/local/vsftpdg/pid
echo "Starting vsftpdg ..."
nohup java -jar /usr/local/vsftpdg/vsftpdg_server.jar /usr/local/vsftpdg 2>> /dev/null >> /dev/null &
echo $! > /usr/local/vsftpdg/pid
echo "Vsftpdg started ..."
else
echo "Vsftpdg is not running ..."
fi
;;
esac
Now save the file to “yourprogramname.sh” (for my purpose vsftpdg) and copy it to the daemon folder (run the code bellow).
sudo mv vsftpdg.sh /etc/init.d/vsftpdg
sudo chmod +x /etc/init.d/vsftpdg
sudo update-rc.d vsftpdg defaults
And that’s it, you’re finished, your Java daemon is now set to start everytime your turn your PC on. You can easily stop it by typing this in the terminal (replace vsftpdg with your own program):
sudo /etc/init.d/vsftpdg stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment