On Mac, kill the process regularly according to the process name / restart the process by the way of Daemons

Reference blogger article https://blog.csdn.net/u010976445/article/details/50819287

To kill and restart pycharm program under Mac as an example to write script, using Python 3.6 environment

1, Kill process

ps -ef | grep pycharm | awk '{print $2}' | xargs kill -9  # After ps -ef | grep pycharm | awk '{print  }' | xargs is executed, all process numbers containing pycharm will be listed

2, Start process

/Applications/PyCharm.app/Contents/MacOS/pycharm

3, Create a shell file, assuming the name is not re|s.sh, the content is as follows, the function is to kill the process, delay 5 s, and start the process (||and & & difference, refer to the blogger's blog https://www.cnblogs.com/clam/archive/2012/12/17/2821684.html)

#!/bin/bash

echo 're start' `date` >> /Users/007/Desktop/test/log.log &&  # Script start time recorded in log

ps -ef | grep pycharm | awk '{print $2}' | xargs kill -9 ||  # To kill a process, you can omit | (use |; instead of & &, because the command will run with a kill: xxxx: No such process error. |, which means to execute the next statement after an error, and & & execute the next statement without an error)

sleep 5s &&  # Delay 5s

/bin/bash /Users/007/Desktop/test/re_start.sh &&  # Startup process

echo 're end' `date` >> /Users/007/Desktop/test/log.log  # Write log

4, Create a script to start the program process. Let's say it's named re_start.sh

#!/bin/bash

echo 'start' `date` >> /Users/007/Desktop/test/log.log &&

/Applications/PyCharm.app/Contents/MacOS/pycharm >>/Users/007/Desktop/test/log.log 2>&1 &  # How daemons start

echo 'end' `date` >> /Users/007/Desktop/test/log.log

5, After all are set up, run the re? S.sh file

Note: the. Sh file needs to be added with executable permissions. However, for convenience, chmod 777 xxx.sh is generally used to add all permissions. You can also integrate part of the code in 4 and 5 into a shell script. If you want to schedule, you can use crontab to schedule http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html

# Integrated code

#!/bin/bash

echo 're start' `date` >> /Users/007/Desktop/test/log.log &&  # Script start time recorded in log

ps -ef | grep pycharm | awk '{print $2}' | xargs kill -9 ||  # To kill a process, you can omit | (use |; instead of & &, because the command will run with a kill: xxxx: No such process error. |, which means to execute the next statement after an error, and & & execute the next statement without an error)

sleep 5s &&  # Delay 5s

/Applications/PyCharm.app/Contents/MacOS/pycharm >>/Users/007/Desktop/test/log.log 2>&1 &  # How daemons start

echo 're end' `date` >> /Users/007/Desktop/test/log.log  # Write log

 

Keywords: Pycharm shell crontab Mac

Added by weedo on Mon, 06 Jan 2020 19:23:08 +0200