命令一: crontab
定期执行爬虫,注意: 设置的周期内爬虫应执行完,否则会产生一堆的后台任务进程
在线crontab表达式验证工具
service crond status 查看crontab状态
crond start 开启crond
crontab -e 编辑
sudo select-editor 选择编辑器
crontab -l 显示
/etc/init.d/cron 配置文件
sudo /etc/init.d/cron start 启动
sudo /etc/init.d/cron stop 关闭
sudo /etc/init.d/cron restart 重启
sudo /etc/init.d/cron reload 重新载入配置
ps aux | grep cron 查看cron是否已启动
分 时 日 月 周 命令
* * * * * command
crontab -e 编辑
crontab -l 显示
crontab -r 删除
crontab -i 删除时给提示
命令二: screen
开启会话运行爬虫,退出会话后,断开服务器连接不会中断爬虫执行
screen -S mac 创建mac会话
screen -ls 查看mac会话
screen -r mac 恢复mac会话
screen -d mac 下线mac会话
screen -wipe mac 清除mac会话
解决screen状态为Attached连上不的问题
screen -D -d session_id 下线已打开的会话
ctrl + a + d 退出会话
ctrl + d 关闭会话
命令三: nohup
运行爬虫程序,退出帐户时该进程不会结束
bash test.sh 正常输出
nohup bash test.sh & 后台输出
nohup bash test.sh > out.log 2>&1 & 指定输出
jobs -l 查看任务
ps aux | grep test.sh 查看PID
kill PID 杀死程序
#!/bin/bash
int=1
while(( $int<=100 ))
do
echo $int
let "int++"
sleep 1
done