linux 定时任务,使用shell脚本每3秒执行一次
- LINUX
- 2018-08-11
- 2407
- 0
linux shell脚本 定时任务 每三秒一次
1.shell脚本
- 创建shell脚本
vim seconds.sh
- 在脚本中输入以下内容
#!/bin/bash
for((i=1;i<=20;i++));do
echo "num "${i}
/home/somedir/scripts.sh 2>/dev/null &
sleep 3
done
- 给当前脚本增加读写权限
chmod u+x ./seconds.sh
2.Linux定时器操作
- 编辑crontab
crontab -e
- 在下面增加需要制定的文件
#分 时 日 月 周 执行文件路劲加文件名称 后台运行 不保存文件输出日志
# * * * * * 为每分钟执行一次
* * * * * /seconds.sh
3.比较好的严谨的方案
- 直接在crontab 中写入
#crontab -e 输入一下
* * * * * /three-seconds.sh
* * * * * sleep 3 && /three-seconds.sh
* * * * * sleep 6 && /three-seconds.sh
* * * * * sleep 9 && /three-seconds.sh
* * * * * sleep 12 && /three-seconds.sh
* * * * * sleep 15 && /three-seconds.sh
* * * * * sleep 18 && /three-seconds.sh
* * * * * sleep 21 && /three-seconds.sh
* * * * * sleep 24 && /three-seconds.sh
* * * * * sleep 27 && /three-seconds.sh
* * * * * sleep 30 && /three-seconds.sh
* * * * * sleep 33 && /three-seconds.sh
* * * * * sleep 36 && /three-seconds.sh
* * * * * sleep 39 && /three-seconds.sh
* * * * * sleep 42 && /three-seconds.sh
* * * * * sleep 45 && /three-seconds.sh
* * * * * sleep 48 && /three-seconds.sh
* * * * * sleep 51 && /three-seconds.sh
* * * * * sleep 54 && /three-seconds.sh
* * * * * sleep 57 && /three-seconds.sh
- 脚本命令中直接需要运行的文件
#vim /three-seconds.sh 输入一下内容
#!/bin/bash
/usr/local/php/bin/php /data/artisan schedule:run >> /dev/null
/usr/local/php/bin/php /test.php >> /dev/null