1 Step by step
- Put all your executing code in a separate text file with an arbitrary name such as
foo.sh
and save it in an arbitrary place like /home/tzx/go/src/foo/foo.sh
. - Add
#!/bin/sh
as first line of your code.
Like the following offoo.sh
And do $ chmod 755 foo.sh
- Try executing your
foo.sh
by$ ./foo.sh
to check there are no errors at all. - Provide your
/etc/rc.local
script withfull pathand name of your created script after the sh command like
- Remember to put the above line before the last line of code
exit 0
at the end of the /etc/rc.local
script like
- [Optional] Check first line of /etc/rc.local to be
#!/bin/sh -e
- [Optional] Make your
/etc/rc.local
executable in case it is not already executable by
- Check everything works fine by executing
$ sudo /etc/init.d/rc.local start
. - Test restart your system by
$ sudo reboot
.
2 Reference
- how-can-i-make-rc-local-run-on-startup
- Centos 7关于rc.local脚本命令开机不执行及指定用户启动的解决方法
- nohup不输出日志信息的方法及linux重定向