Adding a string to the start of a line in linux
Hi all,
Although im not really new to Linux I am still a learner to scripting. I simply want to add a new location to my /etc/syslog file but I cant figure out the command. So... I want to add /var/spool/root to my syslog file by using a script (because I want to run this script across multiple servers) to add it in. My syslog is currently showing as: /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron /var/log/sulog { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript } I want it to include the location /var/spool/root as below: /var/spool/mail/root /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron /var/log/sulog { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript } I've searched for a way to do this but I cant find anything for this specific scenario. It needs to be inserted on the 1st line because I believe that's the way the file is read by the logrotate. Is anyone able to offer some advice please? Thanks Dave |
Quote:
A simple Code:
sed -i 's/^\/var\/log\/messages/\/var\/spool\/mairoot \/var\/log\/messages/g' |
In case you need to run things on multiple hosts now or in the future I would suggest using python 'fabric' which makes it far easier to send commands.
To install on centos 7: Code:
yum install python-pip python-devel Code:
from fabric.api import * Code:
# prompts for password |
Yes, "sed" a good way to do it in a script. It's quite flexible. The suggestion above can be made more readable using a different delimiter for the substitution so that the slashes don't have to be escaped.
Code:
sed 's|^/var/log/messages|/var/spool/mailroot /var/log/messages/|' /etc/syslog |
Quote:
|
Quote:
|
Nice. I always forget about the delimiter option in sed.
If we are concentrating on making a more readable sed statement rather than how to apply it to multiple server, my submission is: Code:
# insert /var/log/messages into beginning of first line |
Not sure if we can assume inserting on the first line (up to OP of course), but a slightly shorter sed would be:
Code:
sed 's;^/var/log/messages;/var/spool/mail/root &;' /etc/syslog |
It looks like more of a logrotate than a syslog config file to me
|
Guys, the response on this was awesome and I really appreciate the time you have taken to assist.
Just a few things... yes, this is related to the logrotate, it is a new location that we would like to rotate on multiple servers. My scripting knowledge is unfortunately not very progressive due to my workload of OS and hardware issues... I get the time to quickly provide answers/solutions, but afterwards, not enough time to digest the methods. (Cant believe its been 5 years since I posted the last scripting thread.) From that, I realise that I really need to learn this via a proper structure to understand it. SED looks great and although I have heard of it before, i've not really understood its capabilities. Thanks again, ill mark this as complete as this has set me en-route to my solution..... hopefully my next scripting request will be at a more advanced level. ;) |
All times are GMT -5. The time now is 06:15 AM. |