Linux daemon note (start if the specified process does not exist) and linux note
Recently, I want to create a daemon In debian. The daemon program is always running. I checked it online and found the Crontab mode and script writing mode.
Crontab
Crontab is a built-in system, similar to Windows scheduled task. For more information, see:
"
Debian scheduled execution command Crontab: http://www.tdblog.cn /? Posting = 276
Use
Nano/etc/crontab # After editing the configuration
Root @:~ # Cat/etc/crontab #/etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the 'crontab' # command to install the new version when you edit this file # and files in/etc/cron. d. these files also have username fields, # that none of the other crontabs do. SHELL =/bin/shPATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin: /usr/bin # m h dom mon dow user command17 ***** root cd/& run-parts -- report/etc/cron. hourly25 6 *** root test-x/usr/sbin/anacron | (cd/& run-parts -- report/etc/cron. daily) 47 6 ** 7 root test-x/usr/sbin/anacron | (cd/& run-parts -- report/etc/cron. weekly) 52 6 1 ** root test-x/usr/sbin/anacron | (cd/& run-parts -- report/etc/cron. monthly) # Run my server once every 3 minutes */3 ***** root/bin/bash/abc/mysh/t2.sh
T2.sh content:
Find the process whose program name is "abc" and whose program parameter is "chanshu"
If it does not exist, start.
Note: & indicates that execution continues in the background after exiting.
#!/bin/shps -fe|grep 'abc chanshu' |grep -v grepif [ $? -ne 0 ]then echo "abc chanshu start process....." /disk1/d1/abc chanshu &else echo "\r\n abc chanshu already runing....."fi#####
/Etc/init. d/cron restart # restart cron
"
However, Crontab can be used to specify the number of seconds between months, days, hours, minutes, and times.
You can use Crontab to execute scheduled cleanup tasks, such as regular cleanup logs.
Script implementation
Reference
"
Run a script in linux to monitor a process and start it if it does not exist. Http://blog.csdn.net/rosekin/article/details/15341835
"
After reading this article, I think this method is more flexible.
So my script
T3.sh
#! /Bin/sh # Keep running while [1] do ps-fe | grep 'abc chanshu' | grep-v grep if [$? -Ne 0] then # echo "abc chanshu start process ..... "/disk1/d1/abc chanshu & # else # echo" \ r \ n abc chanshu already runing ..... "fi # Run sleep 10 done in 10 seconds &#####
T3.sh is always executed in the background, and a scan task is executed every 10 seconds. Why scan the program and scan program parameters? Different parameters allow different division of labor for the same program.
Put t3.sh into the startup item, and then OK.
Debian auto-start
There are many ways to edit the/etc/rc. local configuration.
Save the file and restart the system.
# The Code is as follows: sudo vi/etc/rc. local # Add the software startup command before exit 0. For example:/disk1/aaa/t3.sh
Now, a simple daemon setting method is complete.