调用shell命令

叹号!

 
在linux上执行一个 ls ,直接在命令行输入ls,回车 

在Jupyter中,前面加一个! 
!ls 

jupyter中后台执行脚本

python while+sleep实现定时任务

 
vim test.py 
import time,os
import datetime
def pull_file(nearest_days = 1):
    """拉取文件 
    nearest_days = 1  #不包含当天
    """
    for i in range(1,nearest_days+1):
        day = datetime.datetime.now() - datetime.timedelta(days=i)
        day = day.strftime("%Y-%m-%d")
        print(day)
        os.system(f"echo '{day}' >> /tmp/a.log")
        
while True:
    pull_file(nearest_days = 1)
    time.sleep(1*20)
    

shell脚本中调用python脚本

 
vim test.sh 
# 查询进程
process=`ps -ef |grep python|grep test.py |grep -v grep |awk '{print $2}'`

# 如果进程存在,则结束该进程
if [ -n "$process" ]; then
    kill -0 $process
    sleep 3s
fi
nohup python test.py > ./cron.log &2>&1 &

 
# 查询进程
process=`ps -ef |grep python|grep test.py |grep -v grep |awk '{print $2}'`
killall test.py
nohup python test.py > ./cron.log &2>&1 &

jupyter中代码

 
import os
os.system("sh test.sh")

输出0表示杀掉了进程
输出1表示不存在该进程 

参考