Use login script bash script

I have a bash script from which I need to write logs. I am currently using a block like below

#log stop request { local now=$(date +"%a %b %d %Y %H:%M:%S") printf "batch stop request successful. \n" printf " Time :: %s\n" "$now" printf " PID of process :: %s\n" ${PID[0]}" } >> "${mylogfile}" 

where the mylogfile variable will have the log file name.

The problem with this approach is that when you run 2 or more instances, the logs tend to be confused with recording from the instances included in the rotation.

Please note that I used the block, thinking that this would cause the log to be written to the file at a time, which avoids the problem.

I saw the logger command from the Vivek Gite post . but the problem is that it does not write to a file that I can specify, rather, / var / log / message does it.

Any help is greatly appreciated.

Thank you and respectfully Sibi

+4
source share
4 answers

POSIX (IEEE Std 1003.1-2001) does not define the behavior of parallel write () syscalls that send data to the same file, so you may get different results depending on your platform. You can try to combine all printfs into one, hoping that this will work, but even if this happens, there is no guarantee that it will be in the future or on another platform.

Instead of using concurrency control and cleanup to ensure write consistency, you can send messages to a third process that will write your log messages sequentially to a file on behalf of all processes. In fact, this is what is done with logger and syslog in the post you specified. The logger command does not send messages to /var/log/messages . It sends log messages to syslog , which can be configured to save log messages anywhere. However, administrative privileges are usually required to change this configuration.

If you cannot or do not want to use syslog , you can also use netcat as a log server. Run this to multiplex all incoming log messages from all scripts into a file (this task should constantly run in the background all the time, you can also run it in screen ):

 nc -luk localhost 9876 > shared_log_file & 

(port 9876 is just an example) and log in to each script as follows:

 printf "Log message\n" > /dev/udp/localhost/9876 

You can also use your own UDP server instead of netcat (like this one ).

+3
source

A block does not group printfs in any way, as you have discovered. However, you can do all of these commands in a sub-shell (between brackets), and then redirect the output of the sub-shell!

+2
source

Trying to put 3 printfs on one line, i.e.

 printf "Epsilon batch stop request successful. \n Time :: %s\n PID of process :: %s\n" "$now" ${PID[0]} 

You can still alternate.

Why not start each instance with its own log file using $ {PID} in the name of your log file to save them separately? i.e.

  >> "${mylogfile}.${PID}" 

Edit

As you rely on the registrar, see the man page:

 logger(1) - Linux man page Name logger - a shell command interface to the syslog(3) system log module Synopsis logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message ...] Description Logger makes entries in the system log. It provides a shell command interface to the syslog(3) system log module. Options: -i' Log the process id of the logger process with each line. -s' Log the message to standard error, as well as the system log. -f file Log the specified file. ..... 

It looks like what you want.

Hope this helps.

+1
source

Depending on the nature of your logging, it may be well suited for a syslog, not its personal journal. In this case, logger can be a very good alternative to manual logging.

+1
source

Source: https://habr.com/ru/post/1383113/


All Articles