文章目录
nohup与&后台运行
nohup:不挂断的运行-nohup Command [ Arg … ] [ & ]
runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out. Synopsis:
官方是这么介绍的:在忽略挂起信号的情况下运行给定的命令,以便在注销后命令可以在后台继续运行。
可以这么理解:不挂断的运行,注意并没有后台运行的功能,就是指,用nohup 运行命令可以是命令永远运行下去。
如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。nohup就是不挂起的意思( no hang up)。
& 指后台运行
一般两者一起使用:
nohup command &
nohup python -u /root/projects/ymt_log/OpenSpider.py > /root/logs/ymt_topic_.log 2>&1 &
-u 可以输出打印
&& 与 &
& 表示任务在后台执行,如要在后台运行redis-server,则有 redis-server &
&& 表示前一条命令执行成功时,才执行后一条命令 ,如 echo '1‘ && echo ‘2’
crond系统定时任务
crond服务管理
重新启动crond服务
[root@hadoop101 ~]# service crond restart
crontab 定时任务设置
真实案例
[root@iz2zegwrf3ctt7wu23ymouz ~]# crontab -l
# minute hour day month week command
SHELL=/bin/bash
# 每个一小时,清理缓存
* */1 * * * cd /root/ && ./clear_cache
# 每天0点10分,先到News文件夹后,运行py文件,覆盖log日志
10 00 * * * cd /root/projects/News && nohup /root/anaconda3/bin/python3 -u sougo_wechat_delta.py > /root/logs/news.log 2>&1 &
00 01 * * * cd /root/projects/tyc/tyc/spiders/news && nohup /root/anaconda3/bin/python3 -u 2_1_news_info_days.py > /root/logs/tyc.log 2>&1 &
10 01 * * * cd /root/projects/qichacha/qichacha/spiders/company_news && nohup /root/anaconda3/bin/python3 -u spider_requests_yiling_zl.py > /root/logs/qcc.log 2>&1 &
2>&1
文件描述符0 1 2:0 表示键盘输入 、1表示屏幕输出 、2表示错误输出
把标准出错重定向到标准输出,然后扔到/DEV/NULL下面去。通俗的说,就是把所有标准输出和标准出错都扔到垃圾桶里面。
例如:command >out.file 2>&1 &
command >out.file
是将command
的输出重定向到out.file
文件中,即输出内容不打印到屏幕上,而是输出到out.file
文件中。 2>&1
是将标准出错重定向到标准输出,这里的标准输出已经重定向到了out.file
文件,即将标准出错也输出到out.file
文件中。最后一个&
, 是让该命令在后台执行。