先抛出几个常用的组合命令,后台实行任务和标准缺点输出重定向到标准输出自定义日志文件。
实行后台运行任务命令nohup + 自动运行命令 + & = 后台自动运行命令
nohup /export/install_pkgs/core-agent/agent/bin/falcon-agent -c /export/install_pkgs/core-agent/agent/config/cfg.json &
查看后台运行命令
[root@scm core-agent]# jobs -l[1]+ 126018 Running nohup /export/install_pkgs/core-agent/agent/bin/falcon-agent -c /export/install_pkgs/core-agent/agent/config/cfg.json &
标准缺点输出重定向到日志文件
docker exec php php /var/www/html/jd-api/yii light/light > /data0/allinone/php/applogs/crontab.log 2>&1
方法1、通过rc.local添加开机自启动任务
把稳:有的同学会疑问/etc/rc.local文件,编辑rc.local文件可以吗?答案是可以的,通过ll命令可以查看文件属性,可以创造/etc/rc.local 是/etc/rc.d/rc.local的软链接,编辑的是同一个文件。

[root@scm etc]# ll rc.locallrwxrwxrwx. 1 root root 13 Jun 2 16:21 rc.local -> rc.d/rc.local
首先修正rc.local的实行权限
chmod +x /etc/rc.d/rc.local编辑rc.local文件添加任务,命令中的路径利用绝对路径
编辑rc.local文件添加任务,命令中的路径利用绝对路径
nohup /export/install_pkgs/core-agent/agent/bin/falcon-agent -c /export/install_pkgs/core-agent/agent/config/cfg.json &
接下来实行rc.local命令
source /etc/rc.d/rc.local
方法2、 通过systemctl来掌握做事启动,下面以添加一个set_write_same service为例
新建做事器文件/etc/systemd/system/set_write_same_centos_v7.service;
编辑set_write_same_centos_v7.service做事,内容如下:
[Unit]Description="Set the max_write_same_blocks of some scsi disks to 0" [Service]Type=oneshotRemainAfterExit=yesExecStart=/bin/bash -c "/usr/local/bin/setWriteSame 2>&1 /var/log/setWriteName"[Install]WantedBy=multi-user.target
修正做事实行权限和所属用户和分组,一样平常情形下上线的内容常日会限定;
chmod +x /etc/systemd/system/set_write_same_centos_v7.service && chmod +x /usr/local/bin/setWriteSamechown root:root /etc/systemd/system/set_write_same_centos_v7.service
加载做事、启动做事、做事添加开机自启动项。
systemctl daemon-reloadsystemctl start set_write_same_centos_v7systemctl enable set_write_same_centos_v7
systemctl其它命令
systemctl status set_write_same_centos_v7 //查看做事状态systemctl disable set_write_same_centos_v7 //取消开启自启动
方法3、通过chkconfig添加开机自动实行任务,下面做一个大略的做事脚本
新建任务文件名称print_dated,任务内容打印当前韶光;
提示:如下代码5、6行必须填写。
#!/bin/sh## Startup script for print_date##chkconfig: 35 85 15#description: print_date#processname: print_datedcur_date=`date +%Y-%m-%d,%H:%m:%s`echo $cur_date
将任务print_dated复制到/etc/rc.d/init.d目录中;
添加任务、查看任务;
chkconfig --add print-datedchkconfig --list print-dated
验证任务是否添加成功;
[root@scm init.d]# chkconfig --listNote: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'.netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:offprint-dated 0:off 1:off 2:off 3:on 4:off 5:on 6:off
chkconfig常用命令列表
chkconfig --list #列出所有的系统做事chkconfig --add httpd #增加httpd做事chkconfig --del httpd #删除httpd做事chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情形下都是on(开启)的状态chkconfig --list #列出系统所有的做事启动情形chkconfig --list mysqld #列出mysqld做事设置情形chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行做事,--level 35表示操作只在等级3和5实行,on表示启动,off表示关闭chkconfig mysqld on #设定mysqld在各等级为on,“各等级”包括2、3、4、5等级
chkconfig level 运行级别参考
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户事情状态,root权限,用于系统掩护,禁止远程上岸运行级别2:多用户状态(没有联网NFS)运行级别3:完备的多用户状态(有联网NFS),上岸后进入掌握台命令行模式运行级别4:系统未利用,保留运行级别5:X11掌握台,上岸后进入图形GUI模式运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
恭喜你Get到三种添加开机自启动的方法,快去试试吧!