Linux script sharing: automatic shutdown and computing task management

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.