Write to custom log file from a Bash script
In Linux, I know how to write a simply message to the /var/log/messages file, in a simple shell script I created:
I want to stop throwing messages into the default /var/log/messages file, and create my own. I tried this:
#!/bin/bash logger "have more fun" > /var/log/mycustomlog
It still logs to /var/log/messages . It did create the /var/log/mycustomlog , but it’s empty. Anyone see what I’m missing?
Redirecting standard output doesn’t work because logger doesn’t write to standard output; it writes to the file configured by syslog(3) to receive log messages.
5 Answers 5
logger logs to syslog facilities. If you want the message to go to a particular file you have to modify the syslog configuration accordingly. You could add a line like this:
and restart syslog. Then you can log like this:
logger -p local7.info "information message" logger -p local7.err "error message"
and the messages will appear in the desired logfile with the correct log level.
Without making changes to the syslog configuration you could use logger like this:
logger -s "foo bar" >> /var/log/mycustomlog
That would instruct logger to print the message to STDERR as well (in addition to logging it to syslog), so you could redirect STDERR to a file. However, it would be utterly pointless, because the message is already logged via syslog anyway (with the default priority user.notice ).
For anyone else who was confused at the use of the number 2 after the string in the last example, 1 redirects stdout, and 2 redirects stderr. More info available here: tldp.org/LDP/abs/html/io-redirection.html
And for anyone who doesn’t know how to edit the syslog configuration, here it is on Ubuntu: askubuntu.com/questions/42152/where-is-syslog-conf
What does the — before the file path indicate? I’ve tried googling around but can’t find anything about it. Is it required?
@chepner make a good point that logger is dedicated to logging messages.
I do need to mention that @Thomas Haratyk simply inquired why I didn’t simply use echo .
At the time, I didn’t know about echo, as I’m learning shell-scripting , but he was right.
My simple solution is now this:
#!/bin/bash echo "This logs to where I want, but using echo" > /var/log/mycustomlog
The example above will overwrite the file after the >
So, I can append to that file with this:
#!/bin/bash echo "I will just append to my custom log file" >> /var/log/customlog
- on a side note, it’s simply my personal preference to keep my personal logs in /var/log/ , but I’m sure there are other good ideas out there. And since I didn’t create a daemon, /var/log/ probably isn’t the best place for my custom log file. (just saying)
I agree that this is a good approach but the log file will keep growing and there is no mechanism of archiving or deleting old logs. If you log frequently, then this can take considerable amount of storage.
There’s good amount of detail on logging for shell scripts via global varaibles of shell. We can emulate the similar kind of logging in shell script: http://www.cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html The post has details on introdducing log levels like INFO , DEBUG, ERROR. Tracing details like script entry, script exit, function entry, function exit.
If you see the man page of logger:
LOGGER(1) BSD General Commands Manual LOGGER(1)
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.
It Clearly says that it will log to system log. If you want to log to file, you can use «>>» to redirect to log file.
This is an incorrect interpretation of the man page. logger uses syslog or (in the case of most Linux systems) rsyslog which can be configured to place log messages where ever you like.
I did it by using a filter. Most linux systems use rsyslog these days. The config files are located at /etc/rsyslog.conf and /etc/rsyslog.d .
Whenever I run the command logger -t SRI some message , I want «some message» to only show up in /var/log/sri.log .
To do this I added the file /etc/rsyslog.d/00-sri.conf with the following content.
# Filter all messages whose tag starts with SRI # Note that 'isequal, "SRI:"' or 'isequal "SRI"' will not work. # :syslogtag, startswith, "SRI" /var/log/sri.log # The stop command prevents this message from getting processed any further. # Thus the message will not show up in /var/log/messages. # & stop
Then restart the rsyslogd service:
systemctl restart rsyslog.service
Here is a shell session showing the results:
[root@rpm-server html]# logger -t SRI Hello World! [root@rpm-server html]# cat /var/log/sri.log Jun 5 10:33:01 rpm-server SRI[11785]: Hello World! [root@rpm-server html]# [root@rpm-server html]# # see that nothing shows up in /var/log/messages [root@rpm-server html]# tail -10 /var/log/messages | grep SRI [root@rpm-server html]#
where is /var/log/messages
I have several sources saying that /var/log/messages has system log message but I don’t have that file (Ubuntu 11, Debian). Is the messages file somewhere else now? I tried using locate to look for it but didn’t see it.
1 Answer 1
Ubuntu no longer uses the /var/log/messages file by default. The same information is available in the file /var/log/syslog . You can re-enable logging to /var/log/messages if you would like.
Syslog is a standard logging facility. It collects messages from various programs, including the kernel. It is usually configured to store these messages by default. How it stores these messages is generally distribution-dependant. /var/log/messages is generally used to store non-critical messages while /var/log/syslog stores everything, including critical and non-critical messages.
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Opening a .msg file in Ubuntu [closed]
Questions seeking product, service, or learning material recommendations are off-topic because they become outdated quickly and attract opinion-based answers. Instead, describe your situation and the specific problem you’re trying to solve. Share your research. Here are a few suggestions on how to properly ask this type of question.
Someone sent me a .msg file on Skype, and somehow I just can’t open this in Ubuntu. Is there any app in which I could open the .msg file?
This could be many things. what type is this file? There are several different programs using the msg extension.
10 Answers 10
This is an MS-Outlook format. There is a command line tool called MSGConvert (see www.matijs.net/software/msgconv) which converts .msg files into .eml. You can open those with Thunderbird or Evolution. On Ubuntu you should be able to install the tool using
sudo apt-get install libemail-outlook-message-perl libemail-sender-perl
to convert every file in a directory at once. MSGConvert will produce copies of your .msg-files with the suffix .msg.eml. Regardless, your friend should learn how to send content properly.
Somehow msgconvert didn’t appear in the path, but the following worked: perl -we ‘use Email::Outlook::Message; print Email::Outlook::Message->new(shift)->to_email_mime->as_string’ foo.msg >bar.eml
Just to note, on debian based systems the msgconvert script isn’t in the package. You can get it from the repo however here: github.com/mvz/email-outlook-message-perl
Using the msgconvert tool from github.com/mvz/email-outlook-message-perl, the command line ./msgconvert file.msg produces nothing. You have to use ./msgconvert —outfile file.eml file.msg .
If you already did apt-get install libemail-outlook-message-perl , you don’t need to do anything more. That package already contains /usr/bin/msgconvert , at least on Debian 8 Jessie.
Use Microsoft one drive to open .msg files online via browser:
Sign in to your Microsoft onedrive account and upload the .msg file. After upload, you can click on it to view the file contents.
The advantage over other answers is you won’t be sharing your file with third party file converters.
Note: This method is Non-OS specific and can be applied in any OS.
@Jaakko .msg files are email conversation thread. I coundn’t find any sense of editing it. I think you can copy text content for forwarding mail purposes
despite that fact this is not a solution for the problem, in some particular cases your suggestion works very good!
@radistao thanks for the compliment. I say you don’t have to convert the file to open .msg you just need to use Microsoft tools. for ubuntu, it’s only in web and an electron version i.e nothing but the web. & yeah it definietly solves the problem of viewing .msg and therefore it can be treated as solution 😛
After opening a .msg file on the OneDrive (using current Google Chrome) I can read the message body but I don’t see a way to open attachments contained in the message and listed on the Attachments line.
It’s not perfect but you can import .msg file with Mozilla Thunderbird (it works with on 52.1.1 on my Linux Mint). I had some encoding error but you can globally read the content.
In Thunderbird you click on File > Open > Saved message and select your .msg file.
This kind of works. I can see a plain text message at the top with a lot of issues under it but I think it has got the text content of the email right.
There is no encoding error it simply than the encoding of the Outlook file (*.msg) is ANSI. I have the following in my .bash_profile : function msgread() < msgconvert --outfile - "$1" | iconv -f ms-ansi -t utf-8 | less >
Worked great for a couple of old Outlook messages I had failed to convert over. The messages were text only. Thanks!
Another free and cross-platform tool for extracting the contents of an Outlook msg file from the command line is msg-extractor.
pip install extract-msg extract_msg file.msg
Please note that while package name uses hyphen, command name uses underscore.
I ran across such a file as well (provided to me by a colleague who saved an email message in Microsoft Outlook). file(1) identifies the .msg file like so:
foo.msg: Composite Document File V2 Document
Georg Jung’s answer regarding Matijs van Zuijlen’s perl-based msgconvert(1) utility steered me in the right direction. Although my system does not at the time of this writing have the msgconvert utility packaged, the install instructions on Matijs’ web page indicate using cpan as one way to install it:
cpan -i Email::Outlook::Message
I came to this post searching for an answer, found the above link which did the job for me. Hence wanted to share here.
If you are concerned about privacy, you can buy their desktop version and convert it.
I also downvote this «solution», because it violates privacy. .msg files sometimes contain mail threads, which must be kept confidential.
I don’t get where the tool violates privacy. It’s just a wrong statement. It’s the same saying Winrar violates privacy because sometimes .rar files contain files which may be confidential. Or an SQLite client violates privacy because you can read Skype conversations from .db file.
@AndreFigueiredo: It’s the difference between an online tool and a local program. When you process data with WinRAR or any other program on your computer, the data stay on your computer (unless it’s infected with spyware). But with this “coolutils” solution, you upload your file to their website and then download the result. Do they immediately delete your data off their servers? Are their personnel allowed to look at your data? Do they sell your data to other people? Once you give it to them, it’s out of your control.
People should convert .msg files to smth like pdf before sending to not get poeple such problems opening. Most computers can open pdf without installing anything additionaly or sending to webservice like this.