脚本操作远程linux服务器


scrapyer@spider0:~$ nohup python3 -u test1.py > log.log 2>&1 &
[1] 720875
scrapyer@spider0:~$ ps -ef |grep test1.py
scrapyer  720875  720633  0 17:43 pts/1    00:00:00 python3 -u test1.py
scrapyer  720889  720633  0 17:43 pts/1    00:00:00 grep test1.py
scrapyer@spider0:~$ ps -ef |grep test1.py |cut -c 9-15 
  72087
  72096
scrapyer@spider0:~$ ps -ef |grep test1.py |cut -c 9-16 
  720875
  720986
scrapyer@spider0:~$ cat log.log 
nohup: ignoring input
2021-05-17 17:43:18
2021-05-17 17:43:23
2021-05-17 17:43:28
2021-05-17 17:43:33
2021-05-17 17:43:38
2021-05-17 17:43:43
2021-05-17 17:43:48
2021-05-17 17:43:53
2021-05-17 17:43:58
2021-05-17 17:44:03
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -w grep|cut -c 9-16 
  721115
scrapyer@spider0:~$ ps -ef |grep tesd1
scrapyer  721155  720633  0 17:44 pts/1    00:00:00 grep tesd1
scrapyer@spider0:~$ ps -ef |grep test1
scrapyer  720875  720633  0 17:43 pts/1    00:00:00 python3 -u test1.py
scrapyer  721180  720633  0 17:45 pts/1    00:00:00 grep test1
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -w grep
scrapyer  721217  720633  0 17:45 pts/1    00:00:00 grep test1.py
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep
scrapyer  720875  720633  0 17:43 pts/1    00:00:00 python3 -u test1.py
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep |cut -c 9-16
  720875
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep |cut -c 9-16 
  720875
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep |cut -c 9-16 |xargs kill -stop

[1]+  Stopped                 nohup python3 -u test1.py > log.log 2>&1
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep |cut -c 9-16 |xargs kill -start
kill: (720875): Invalid argument
scrapyer@spider0:~$ cat log.log 
nohup: ignoring input
2021-05-17 17:43:18
2021-05-17 17:43:23
2021-05-17 17:43:28
2021-05-17 17:43:33
2021-05-17 17:43:38
2021-05-17 17:43:43
2021-05-17 17:43:48
2021-05-17 17:43:53
2021-05-17 17:43:58
2021-05-17 17:44:03
2021-05-17 17:44:08
2021-05-17 17:44:13
2021-05-17 17:44:18
2021-05-17 17:44:23
2021-05-17 17:44:28
2021-05-17 17:44:33
2021-05-17 17:44:38
2021-05-17 17:44:43
2021-05-17 17:44:48
2021-05-17 17:44:53
2021-05-17 17:44:58
2021-05-17 17:45:03
2021-05-17 17:45:08
2021-05-17 17:45:13
2021-05-17 17:45:18
2021-05-17 17:45:23
2021-05-17 17:45:28
2021-05-17 17:45:33
2021-05-17 17:45:38
2021-05-17 17:45:43
2021-05-17 17:45:48
2021-05-17 17:45:53
2021-05-17 17:45:58
2021-05-17 17:46:03
2021-05-17 17:46:08
2021-05-17 17:46:13
2021-05-17 17:46:18
2021-05-17 17:46:23
2021-05-17 17:46:28
scrapyer@spider0:~$ tail -n 10 log.log 
2021-05-17 17:45:43
2021-05-17 17:45:48
2021-05-17 17:45:53
2021-05-17 17:45:58
2021-05-17 17:46:03
2021-05-17 17:46:08
2021-05-17 17:46:13
2021-05-17 17:46:18
2021-05-17 17:46:23
2021-05-17 17:46:28
scrapyer@spider0:~$ tail -n 10 log.log 
2021-05-17 17:45:43
2021-05-17 17:45:48
2021-05-17 17:45:53
2021-05-17 17:45:58
2021-05-17 17:46:03
2021-05-17 17:46:08
2021-05-17 17:46:13
2021-05-17 17:46:18
2021-05-17 17:46:23
2021-05-17 17:46:28
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep |cut -c 9-16 |xargs kill -reload
kill: invalid argument r

Usage:
 kill [options] <pid> [...]

Options:
 <pid> [...]            send signal to every <pid> listed
 -<signal>, -s, --signal <signal>
                        specify the <signal> to be sent
 -l, --list=[<signal>]  list all signal names, or convert one to a name
 -L, --table            list all signal names in a nice table

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see kill(1).
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep |cut -c 9-16 |xargs kill -cont
scrapyer@spider0:~$ tail -n 10 log.log 
2021-05-17 17:45:48
2021-05-17 17:45:53
2021-05-17 17:45:58
2021-05-17 17:46:03
2021-05-17 17:46:08
2021-05-17 17:46:13
2021-05-17 17:46:18
2021-05-17 17:46:23
2021-05-17 17:46:28
2021-05-17 17:52:03
scrapyer@spider0:~$ tail -n 10 log.log 
2021-05-17 17:45:48
2021-05-17 17:45:53
2021-05-17 17:45:58
2021-05-17 17:46:03
2021-05-17 17:46:08
2021-05-17 17:46:13
2021-05-17 17:46:18
2021-05-17 17:46:23
2021-05-17 17:46:28
2021-05-17 17:52:03
scrapyer@spider0:~$ tail -n 10 log.log 
2021-05-17 17:45:53
2021-05-17 17:45:58
2021-05-17 17:46:03
2021-05-17 17:46:08
2021-05-17 17:46:13
2021-05-17 17:46:18
2021-05-17 17:46:23
2021-05-17 17:46:28
2021-05-17 17:52:03
2021-05-17 17:52:08
scrapyer@spider0:~$ ps -ef |grep test1.py |grep -v grep |cut -c 9-16 |xargs kill -stop

[1]+  Stopped                 nohup python3 -u test1.py > log.log 2>&1
scrapyer@spider0:~$ vim ps_stop.sh
scrapyer@spider0:~$ bash ps_stop.sh 

scrapyer@spider0:~$ bash ps_stop.sh 111
111
scrapyer@spider0:~$ vim ps_stop.sh
scrapyer@spider0:~$ bash ps_stop.sh test1.py
进程名称:test1.py
scrapyer  720875  720633  0 17:43 pts/1    00:00:00 python3 -u test1.py
scrapyer  723483  720633  0 17:59 pts/1    00:00:00 bash ps_stop.sh test1.py
是否暂停进程:test1.py(y/n)n
取消暂停进程!
scrapyer@spider0:~$ bash ps_stop.sh test1.py
进程名称:test1.py
scrapyer  720875  720633  0 17:43 pts/1    00:00:00 python3 -u test1.py
scrapyer  723569  720633  0 17:59 pts/1    00:00:00 bash ps_stop.sh test1.py
是否暂停进程:test1.py(y/n)yes

[2]+  Stopped                 bash ps_stop.sh test1.py
scrapyer@spider0:~$ chmod +x ps_stop.sh 
scrapyer@spider0:~$ ./ps_stop.sh test1
进程名称:test1
scrapyer  720875  720633  0 17:43 pts/1    00:00:00 python3 -u test1.py
scrapyer  723569  720633  0 17:59 pts/1    00:00:00 bash ps_stop.sh test1.py
scrapyer  723723  720633  0 18:00 pts/1    00:00:00 /bin/bash ./ps_stop.sh test1
是否暂停进程:test1(y/n)n
取消暂停进程!
 


#!/bin/bash
  
psname=$1
echo "进程名称:$psname"
ps -ef |grep $psname |grep -v grep
read -p "是否暂停进程:$psname(y/n)" choose
if [ "$choose" = "y" ] || [ "$choose" = "yes" ];
then
ps -ef |grep $psname |grep -v grep |cut -c 9-16 |xargs kill -stop
else
        echo "取消暂停进程!"
fi

#encoding=utf-8
import time

while 1:
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
    time.sleep(5)
                                                                                                                                                   


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值