一.定时任务
1.编辑任务
crontab -e
#实际的地址在/var/spool/cron
#编写任务
1 * * * * /myproject/sql.sh
2.查询任务
crontab -l
3.Cron表达式
* * * * *
:每分钟都运行0 * * * *
:每小时的第0分钟运行0 2 * * *
:每天凌晨2点运行0 2 * * 1
:每周一的凌晨2点运行
二.开机自启
1.修改 /etc/rc.d/rc.local 文件
将需要执行的.sh脚本文件路径添加到rc.local末尾。
2.使用 crontab
在定时任务中有特殊的reboot任务
@reboot /root/auto_run_script2.sh
3.使用 systemd 服务
创建.server文件,并且放到/etc/systemd/system/目录下。
[Unit]
Description=Run a Custom Script at Startup
After=default.target
[Service]
ExecStart=/root/auto_run_script3.sh
[Install]
WantedBy=default.target
更新 systemd 配置文件
systemctl daemon-reload
启动服务
systemctl enable auto_run_script3.service
三.其他快捷键
1.软连接
ln -s /pwgj/my.sh /bin/pw
命令的含义是将 /pwgj/my.sh
文件创建一个符号链接,并命名为 /bin/pw
,即在 /bin
目录下创建一个名为 pw
的符号链接,指向 /pwgj/my.sh
文件。全局可以直接使用pw。
2.别名
#这里使用别名(写入到bashrc文件) echo "alias pw='bash /pwgj/my.sh'">>"/etc/bashrc" source "/etc/bashrc"
3.登录提示
#登陆提示需要写入到motd里面 echo -e "欢迎使用pw工具箱\n命令:pw\n">"/etc/motd"