Linux后台命令的使用说明
1)ctrl+Z:停止当前进程
首先先将一个程序运行起来,这个时候如果你需要去干别的事情,需要暂停运行,可以使用ctrl+Z:
user@mine:/opt/user/pytorch-gender$ python train_debug.py --debugFile=./debug {'debugFile': './debug'} Epoch 0/199 ---------- ^Z [1]+ Stopped python train_debug.py --debugFile=./debug
从上面可以看见,这个程序已经已经停止了,状态为Stopped
2)jobs:用于查看正在运行的命令
user@mine:/opt/user/pytorch-gender$ jobs [1]+ Stopped python train_debug.py --debugFile=./debug
前面的编号[1]是命令编号
3)bg 命令编号:把程序调度到后台执行:
user@mine:/opt/user/pytorch-gender$ bg 1 [1]+ python train_debug.py --debugFile=./debug & user@mine:/opt/user/pytorch-gender$ jobs [1]+ Running python train_debug.py --debugFile=./debug &
然后我们可以看见该命令在后台运行起来了,状态为Running,命令后的&标志就是把命令放在后台运行的意思
这个时候该命令生成的返回信息会自己打印出来:
user@mine:/opt/user/pytorch-gender$ train Loss: 0.4611 Acc: 0.7824 val Loss: 0.1882 Acc: 0.9340 Epoch 1/199 ---------- user@mine:/opt/user/pytorch-gender$ train Loss: 0.3271 Acc: 0.8578 val Loss: 0.1845 Acc: 0.9260 Epoch 2/199 ----------
不影响你运行其他的命令,你就输入你的命令回车即可
当然,如果你不想让输出显示在控制台中,那就在运行时指明将输出信息写入日志文件:
user@mine:/opt/user/pytorch-gender$ python train_debug.py --debugFile=./debug >> gender_log_debug_1.out
打开另一个窗口查看日志文件为:
user@mine:/opt/user/pytorch-gender$ cat gender_log_debug_1.out {'debugFile': './debug'} Epoch 0/199 ----------
然后这个时候如果你想进行调试,即pytorch Debug —交互式调试工具Pdb (ipdb是增强版的pdb)-1-在pytorch中使用,那么你在本地生成文件夹debug后,再查看日志文件变为:
user@mine:/opt/user/pytorch-gender$ cat gender_log_debug_1.out {'debugFile': './debug'} Epoch 0/199 ---------- train Loss: 0.4507 Acc: 0.7919 val Loss: 0.1578 Acc: 0.9420 Epoch 1/199 ---------- train Loss: 0.3201 Acc: 0.8576 val Loss: 0.1069 Acc: 0.9540 Epoch 2/199 ---------- --Call-- > /home/mine/anaconda3/lib/python3.6/site-packages/torch/autograd/grad_mode.py(129)__exit__() 128 --> 129 def __exit__(self, *args): 130 torch.set_grad_enabled(self.prev) ipdb> user@mine:/opt/user/pytorch-gender$
这时候你在命令端输入调试命令u:
user@mine:/opt/user/pytorch-gender$ python train_debug.py --debugFile=./debug >> gender_log_debug_1.out u
可见日志文件中变为:
user@mine:/opt/user/pytorch-gender$ cat gender_log_debug_1.out {'debugFile': './debug'} Epoch 0/199 ---------- train Loss: 0.4507 Acc: 0.7919 val Loss: 0.1578 Acc: 0.9420 Epoch 1/199 ---------- train Loss: 0.3201 Acc: 0.8576 val Loss: 0.1069 Acc: 0.9540 Epoch 2/199 ---------- --Call-- > /home/mine/anaconda3/lib/python3.6/site-packages/torch/autograd/grad_mode.py(129)__exit__() 128 --> 129 def __exit__(self, *args): 130 torch.set_grad_enabled(self.prev) ipdb> > /opt/user/pytorch-gender/train_debug.py(151)train_model() 150 import ipdb; --> 151 ipdb.set_trace() 152 ipdb>
如果调用l 123命令:
user@mine:/opt/user/pytorch-gender$ python train_debug.py --debugFile=./debug >> gender_log_debug_1.out u l 123
可见日志文件又变为:
user@mine:/opt/user/pytorch-gender$ cat gender_log_debug_1.out {'debugFile': './debug'} Epoch 0/199 ---------- train Loss: 0.4507 Acc: 0.7919 val Loss: 0.1578 Acc: 0.9420 Epoch 1/199 ---------- train Loss: 0.3201 Acc: 0.8576 val Loss: 0.1069 Acc: 0.9540 Epoch 2/199 ---------- --Call-- > /home/mine/anaconda3/lib/python3.6/site-packages/torch/autograd/grad_mode.py(129)__exit__() 128 --> 129 def __exit__(self, *args): 130 torch.set_grad_enabled(self.prev) ipdb> > /opt/user/pytorch-gender/train_debug.py(151)train_model() 150 import ipdb; --> 151 ipdb.set_trace() 152 ipdb> 118 labels = labels.to(device) # 当前批次的标签输入 119 # print('input : ', inputs) 120 # print('labels : ', labels) 121 122 # 将梯度参数归0 123 optimizer.zero_grad() 124 125 # 前向计算 126 # track history if only in train 127 with torch.set_grad_enabled(phase == 'train'): 128 # 相应输入对应的输出 ipdb>
所以输出和命令输入虽然不在一起,但是并不妨碍功能的实现
4)fg 命令编号:将后台命令调到前台运行
如果我想要对上面的命令进行调试,我就需要将其调到前台,然后再进行调试
user@mine:/opt/user/pytorch-gender$ fg 1 python train_debug.py --debugFile=./debug train Loss: 0.2337 Acc: 0.9059 val Loss: 0.1347 Acc: 0.9400 Epoch 4/199 ---------- train Loss: 0.2040 Acc: 0.9141 val Loss: 0.0962 Acc: 0.9640 Epoch 5/199 ---------- train Loss: 0.1984 Acc: 0.9182 val Loss: 0.0825 Acc: 0.9720 Epoch 6/199 ---------- train Loss: 0.1841 Acc: 0.9218 val Loss: 0.1059 Acc: 0.9640 Epoch 7/199 ---------- train Loss: 0.1868 Acc: 0.9215 val Loss: 0.0668 Acc: 0.9740 Epoch 8/199 ---------- train Loss: 0.1782 Acc: 0.9273 val Loss: 0.0735 Acc: 0.9740 Epoch 9/199 ---------- train Loss: 0.1703 Acc: 0.9291 val Loss: 0.0850 Acc: 0.9680 Epoch 10/199 ---------- train Loss: 0.1596 Acc: 0.9329 val Loss: 0.1114 Acc: 0.9560 Epoch 11/199 ---------- --Call-- > /home/mine/anaconda3/lib/python3.6/site-packages/torch/autograd/grad_mode.py(129)__exit__() 128 --> 129 def __exit__(self, *args): 130 torch.set_grad_enabled(self.prev) ipdb>
5)nohup 命令 & :直接将命令放在后台运行
nohup python train_debug.py --debugFile=./debug &
如果要指定返回信息写入的日志文件log.out:
nohup python train_debug.py --debugFile=./debug >> log.out &