Make-a-startup-script-in-Ubuntu

There are many ways to make a script startup at boot. It’s quite easy to write a script. But the most important things are making it have the execute permission and making sure that your script’s path is correct.

To make it execute, you need to put this command in you terminal

chmod +x your_script.sh

Be sure that you have write a ** absolute path ** in your script or you won’t get what you want. For example, if you want to run a program that you installed yourself (such as shadowsocks) you should write /opt/shadowsocks/shadowsocks not shadowsocks even you can easily run this in your teirminal. This is because the system don’t know what the command mean (when you type a shadowsocks, the system will find where the program is and then execute it.)

Then put your script in /etc/init.d/ for example ( /etc/init.d/your_script.sh ) and next, run this command

This command will make a symble link to rc’x’.d (x = 2,3,4,5,) and it will startup at boot.

This is a sample init_script blow:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          Shadowsocks
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start ssserver at boot time
# Description:       shadowsocks server.
### END INIT INFO
dir=/opt/shadowsocks
case "$1" in
  start)
    nohup /usr/local/bin/ssserver -c ${dir}/config.json > ${dir}/log &
    ;;
  stop)
    pkill ssserver
    ;;
  *)
   echo "Usage:service shadowsocks {start|stop}"
    exit 1
;;
esac

The other method is adding a single command in /etc/rc.local . You can put your command at the bottom of the file. But remember, always put the absolute path in your command.

Sorry about my poor English.

comments powered by Disqus