Editor's note: the bash shell script shared in this article is used to automatically shut down Linux and manage simple computing tasks. Other functions can be extended. Here, we mainly provide an implementation idea.
1. Automatic shutdown script
It is detected at intervals. If a process does not exist, it is shut down and sleep if it exists.
Usage:./Script Name process name sleep time
Note: The permission on the server is generally the root user or sudoer permission )!
Recommended usage: screen./Script Name process name sleep time
#/bin/bash while :do thread_num=`ps -e |grep $1 | wc -l` if [ $thread_num -eq 0 ]; then date >> shutdowntime.log shutdown -h exit else echo "Sleeping $2 second..." sleep $2 fidone
2. computing task management script
Simple computing task management: checks the number of processes with a specific name. If it is not less than the set number, the task is submitted until the number of processes is equal to the set number; if the number of processes is greater than or equal to the set number, sleep for a certain period of time.
Usage:./Script Name program name task count
We recommend that you use screen.
#! /Bin/bash pro_name = $ 1task_num = $ 2cycle_num = $3 I = 1 while ($ I <= $ cycle_num )) do pro_num = $ (ps-A | grep $ pro_name | wc-l) if ($ pro_num <$ task_num )); then echo $ I # Here write the program to run sleep 1 s I =$ ($ I + 1) else echo 'sleeping 600s' sleep 600 s fidone
In addition, you can control the number of tasks in the cycle for the time period:
Core_num = 'cat/proc/cpuinfo | grep 'core id' | wc-l' time_hour = 'date + % H' if ($ time_hour> = 23 )) | ($ time_hour <7); then task_num = $ core_num # This period from PM to AM, task else task_num = $ ($ core_num/2) according to the number of cores on the machine. # The other time only occupies half of the core for task fi.
After executing the command, you can add the command to shut down the system.
Original article: http://blog.sina.com.cn/s/blog_59cf67260100muoj.html