[Linux]开机自启
1 方式1:systemctl
- 开启开机自启服务
- systemctl enable <服务名>
- 关闭开机自启服务
- systemctl disable <服务名>
- 查看开机自启服务状态
- systemctl list-unit-files | grep <服务名>
#[demos]
systemctl enable firewall
systemctl disable firewall.service
systemctl disable sshd
systemctl enable sshd
systemctl list-unit-files | grep sshd
2 方式2:开机自启脚本
2.1 开机自启脚本编写与配置
以消息队列RabbitMQ为例
- 编写start_rabbitmq.sh
vim /usr/local/rabbitmq/sbin/start_rabbitmq.sh
[编写自启内容]
#!/bin/bash
#erlang
export PATH=$PATH:/usr/rabbitmq/erlang/bin
#rabbitmq
export HOME=/usr/rabbitmq/rabbitmq/
/usr/rabbitmq/rabbitmq/sbin/./rabbitmq-server -detached
-detached:以后台守护进程方式运行
- 给脚本增加可执行权限
chmod +x /usr/local/rabbitmq/sbin/start_rabbitmq.sh
- 编辑开机自启的系统文件
vim /etc/rc.d/rc.local
[编辑内容:↓]
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
#↓我们编写的启动脚本
/usr/local/rabbitmq/sbin/start_rabbitmq.sh
- 给系统脚本添加执行权限
chmod +x /etc/rc.d/rc.local
- 重启,并查看是否生效
shutdown -r now
2.2 开机自启失效的失效/故障解决方法
- 失效错误1:"/bin/bash^M: 坏的解释器: 没有那个文件或目录"
在Linux下编译shell运行脚本的时候出现”/bin/bash^M: 坏的解释器: 没有那个文件或目录“这样的错误如下图。
[解决办法]
使用在终端输入: sed -i 's/\r$//' my-johnnyzen-shell.sh即可
[原因分析]
此shell脚本文件在Windows 下编辑过,在Windows下每一行结尾是\n\r,而Linux下则是\n
sed -i 's/\r$//' make-all-linux-project.sh 会把make-all-linux-project.sh中的行尾的\r替换为空白
3 参考文档
本文作者:
千千寰宇
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!