How can I log to a specific file in linux using lo

2019-04-06 08:26发布

I will run the following script:

#!/bin/bash
./myprogram

#get exit code
exitvalue=$?

#log exit code value to /var/log/messages
logger -s "exit code of my program is " $exitvalue

But I don't want log message to be written in /var/log/messages because I don't have root privileges. Instead I want it to be written to a file in my home directory: /home/myuser/mylog

How should I modify logger command above?

标签: bash logging
5条回答
Viruses.
2楼-- · 2019-04-06 08:58

Unfortunately, you can't.

The logger command is just a thin frontend for the system logger. This means that anything you feed to it goes right over to syslogd, which then does all sorts of processing (like adding timestamp, hostname, priority, tag etc.) as well as complex dispatching to various outlets (various files, sockets, other servers etc.).

It even does bridge permission levels, so in your example you could still log to syslog or user.log, for instance, with no problem, even though you may not modify those files directly.

If logger could log to a file directly, it would either be as dumb as a simple echo one-liner, or would have to reimplement many of the message processing duties done by syslog. Either way, that would be a bad idea.

It would be much better if the standard syslog interface had a way to specify an ad-hoc outlet parameter (like a filename) along with the message, so one could log to custom files without reconfiguring the system (which normal users have no permission to do anyway).

查看更多
Animai°情兽
3楼-- · 2019-04-06 09:01
$ man logger

Logger provides a shell command interface to the syslog(3) system log module.

You'll need to change your syslog configuration if you want it to log things to other places. You could establish a certain facility that has an output file in your home directory, for example. You would need to be root to do that, though.

查看更多
虎瘦雄心在
4楼-- · 2019-04-06 09:08

I don't think you really need to (or want to) involve logger/syslog for this. Simply replace the last line of the script with:

echo "Exit code of my program is $exitvalue" >> /some/file/that/you/can/write/to
查看更多
闹够了就滚
5楼-- · 2019-04-06 09:10

I think your better choice would be to use the date command rather then logger in cases where you don't want to write to the syslog files (and don't have privs to do so).

See "timestamp before an echo" for details on how to use date to prefix a message with a date and write it to a file.

You create a bash function that looks like the following, adjusting the date format string to get what you want:

echo_time() {
    echo `date +'%b %e %R '` "$@"
}

In your bash script, you would then use:

echo_time "Your message here" >> ${LOGFILE}

Which would put the following in your ${LOGFILE} file:

Mar 11 08:40 your message here
查看更多
男人必须洒脱
6楼-- · 2019-04-06 09:10

If you want to use logger so that the message appears both in the system logs and in some file of yours, you might do

  logger -s your message 2> $HOME/somefile

since the -s option to logger also outputs on stderr which is redirected to the file with 2>

查看更多
登录 后发表回答