Linuxscreen prints logs
Requirement: A Background Program of the company is executed under the screen. It can only be viewed in the window after being restored and cannot view all information. The background program needs to be restarted after being updated, check the execution process of the background program to determine whether the update is successful. Now, you need to print the screen running information of the background program to the log and provide it to developers for debugging.
Implementation: 1. output screen logs to a file and grant the developer the permission to read the file;
2. Add the log name with time to easily identify whether the background is restarted successfully using the log name;
3. Delete the original log every time you restart the background to save space and regenerate the log name with time
Implementation process:
Key points: 1. Change the screen configuration file/etc/screenrc to add a line of logfile/root/screenlog/% t-20140905-163549.log at the end
2. parameters need to be added to start the screen command.
/Usr/bin/screen-L-tAa1 aa1-dmS./aa1
Here I implement a script. The log file name will be generated based on time.
#!/bin/bashexport LANG=zh_CN.UTF-8declare -i portkillall screenrm -rf /root/screenlog/*.logecho "logfile /root/screenlog/%t-`date +%Y%m%d-%H%M%S`.log" >> /opt/sjzq/screenrc_temp\cp -rf /opt/sjzq/screenrc_temp /etc/screenrcfor i in `seq 1 6`do n=`pgrep aa$i |wc -l` if [ $n -eq 0 ];then cd /fb$i /usr/bin/screen -L -t aa$i -dmS aa$i ./aa$i >/dev/null 2&1 fidonesed -i '$d' /opt/sjzq/screenrc_temp
Every time you execute guard1.sh, the screen will be restarted. We can see that logs generated under/root/screenlog are composed of session names and time. In this way, we can judge log restart based on time, you can view related logs.
[root@localhost sjzq]# bash guard1.sh [root@localhost sjzq]# ls /root/screenlog/aa1-20140905-165056.log aa2-20140905-165056.log aa3-20140905-165056.log aa4-20140905-165056.log aa5-20140905-165056.log aa6-20140905-165056.log[root@localhost sjzq]# bash guard1.sh[root@localhost sjzq]# ls /root/screenlog/aa1-20140905-165127.log aa2-20140905-165127.log aa3-20140905-165127.log aa4-20140905-165127.log aa5-20140905-165127.log aa6-20140905-165127.log