工作或生活當中,常常存在這樣的需求:將常用的工具、服務做成開機啓動項,省去開機後手動觸發啓動的麻煩。今天在這裏記錄下Ubuntu 18.04上如何設置開機啓動。
一、首先編寫一個可執行的啓動腳本 /root/.runCommandOnBoot.sh
#!/bin/bash
#shadowsocks
/usr/local/bin/sslocal -c /etc/shadowsocks.json -d start
二、設置開機執行該腳本
root@ThinkPad-T450s:~# crontab -e
no crontab for root - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /usr/bin/code
5. /bin/ed
Choose 1-5 [1]: 1 #這裏選擇1
接着出現下面的信息:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
在該文件的末尾追加如下信息
@reboot /root/.runCommandOnBoot.sh
保存並退出。控制檯出現如下信息,表示創建成功。
crontab: installing new crontab