Issue
I want to modify my cronjob by using sed cmd
my cronjob is as below
* * * * * root /apps/filebeat-cybersoc-3/config_updater.sh
i used sed to modify my line so i tape the cmd below
sed -i 's|* * * * * root /apps/filebeat-cybersoc-3/config_updater.sh|* * * * * root /apps/filebeat-cybersoc-3/config_updater.sh > /apps/filebeat-cybersoc-3/logs/config_updater.log|' /etc/cron.d/filebeat-cybersoc-3
i dont get the result as expected i got this :
* * * * * * * * * root /apps/filebeat-cybersoc-3/config_updater.sh > /apps/filebeat-cybersoc-3/logs/config_updater.log
instead of :
* * * * * root /apps/filebeat-cybersoc-3/config_updater.sh > /apps/filebeat-cybersoc-3/logs/config_updater.log
Solution
You're overthinking the problem - all you need is (assuming that you only have that one line in the file)
sed -i 's|$|> /apps/filebeat-cybersoc-3/logs/config_updater.log|' /etc/cron.d/filebeat-cybersoc-3
where s|$|
searches for the end of line and then you just attach the redirect.
As Nahuel pointed out in the comment above: *
is a quantifier in regex, so that won't work without escaping.
Otherwise:
sed -i 's|^\* \* \* \* \* root /apps/filebeat-cybersoc-3/config_updater.sh|* * * * * root /apps/filebeat-cybersoc-3/config_updater.sh > /apps/filebeat-cybersoc-3/logs/config_updater.log|' /etc/cron.d/filebeat-cybersoc-3
Answered By - tink Answer Checked By - David Goodson (WPSolving Volunteer)