Article Title: instance parsing: compile a simple Linux Service Control script. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems, open-source and other basic categories, first compiled a program used as a service, the function is very simple, write the current time to a file every second:
void recordTime(){ const char pa[256] = "//home//projects//testService//recordTime"; ofstream fout; fout.open(pa, ios::app); time_t currTime; struct tm *tp; char buf[256]; while(1) { currTime = time(NULL); tp = localtime(&currTime); strftime(buf, 256, "%B %e, %Y, %H:%M:%S", tp); fout<<"current time is "<
<<
|
Then compiled into an executable file. I named it testService.
Then place a script file in/etc/init. d, which contains the function implementation of the Service Startup, shutdown, and restart:
start(){ echo "start testService" /home/projects/testService/testService & exit 0;}stop(){ echo -n "stop testService" if pkill testService then echo " [ok]" else echo " [failed]" fi}case "$1" instart) start ;;stop) stop ;;restart) stop start ;;*) echo "usage: $0 start|stop|restart" exit 0;esac
|
All functions are clear at a glance. Call the start () function to start the testService program in the background. stop () is used to stop the program. restart () simply executes the stop () and start () functions. When you need to start or stop the service, you only need to give the program a start/stop parameter.
In this way, this simple service is complete.