Write a simple configuration file and log management (shell)

Source: Internet
Author: User

Recently in the design of a Linux system scenario, write a configuration and log management of the upgrade service program.

A total of 4 files, Server a updateserver.conf configuration file and a updateserver script, a updateclinet.conf configuration file and a updateclient script for the client.
The configuration file mainly write some path variables, file name variables, IP address variables, involving the path is best with absolute path. The configuration file is used to provide the user with a modified execution environment and related input information.

The execution script will write how to implement, including, display time, execute program, redirect and so on.

The specific notes are as follows:

01. The script reads another script in two ways:

# First Kind   while read line;  do   # Second, you can add a path     in front of the file " $line "    

After reading, you can refer to the variables directly in the $.

System time display method in Shell:

" +%f%h:%m:%s "

The C language can be displayed as follows:

time_t now; struct TM *Tm_now; time (&= localtime (& now);p rintf ("\n%0 4d-%02d-%02d%02d:%02d:%02d\n", Tm_now->tm_year+1900, tm_now->tm_mon+1 , Tm_now->tm_mday, Tm_now->tm_hour, Tm_now->tm_min, tm_now->tm_sec);        

03. When the program output redirection encountered the problem of unable to redirect the real-time, only the program is finished to write to the file, and the actual need to continue to run the output of the program is recorded in the log file.

After testing, in Linux, C language compiled by the program is not like a shell script line of execution, real-time display, and printf output principle is the use of buffer output stdout, in the case of terminal output, it is a line output mode, that is encountered newline character output, If there is a while (1) or sleep () in the program, it will actually wait until the buffer is full before outputting, so there will be no output on the screen when there is a dead loop.

Post a forum on the Smallnat explanation:

The output from printf () is output to a buffer before it is sent to the screen from the buffer.  1. Force Refresh with Fflush (stdout).  2. The buffer is full.  3. scanf () to fetch the data in the buffer, the buffer is flushed first.  4. When the \n,\r enters the buffer.  5. At the end of the thread, if the line thread also has printf (...); 6. At the end of the program.

Therefore, it is possible to follow a fflush (stdout) behind printf (), and a method is found:

Change stdout default buffer is no buffer, that is, at the beginning of the program to join:
Setvbuf (stdout, (char *) NULL, _iolbf, 0);

can be resolved.

In addition, if you want to display the output results on the screen while redirecting the file, you can use | Tee-a log.

. Socket programming in the server to get the IP address of the client to connect the method:

Accept (socket_ser,&addr,&Addr_len);p rintf ("%s\n", Inet_ntoa ( ADDR.SIN_ADDR));        

05. Exchange two different directories of the same name file, with the MV instruction can do:

MV Bin/A atmpmv a bin/Amv Atmp a

Write a simple configuration file and log management (shell)

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.