我们通过shell窗口运行程序时,由于有的程序长时间运行,直到下班了都还没有返回运行结果。这个时候,我们又不能直接关闭shell窗口,不然前面的时间就白白运行了。

那有什么办法可以先暂停程序,明天再继续运行呢?(不可行,关闭shell窗口后,后台暂停的程序就终止了)

有!

那就是在shell窗口中先暂停,再继续。

暂停操作:ctrl+z  组合键

继续操作:fg    命令

下面我们来做过试验:写一个python脚本程序,打印起始运行时间戳,并记录累计运行的次数,无限运行下去。这样时间就足够长了。

time2show.py 中代码:

import time

print("first time:%s" % time.time())

count_n = 0

while True:

count_n += 1

# 将时间戳int转为时间字符串

timestamp = time.time()

# 转换成localtime

time_local = time.localtime(timestamp)

# 转换成新的时间格式(2016-05-05 20:28:54)

dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)

print("n=%s %s" % (count_n, dt))

time.sleep(2)

运行效果:

dfba6b01ed81081af8778f7bc143e066.png

操作效果

013e42f05e6d2d41e922ac3b7f4cbc05.png

延伸:jobs命令可以查看所有后台暂停的程序。

Logo

更多推荐