I'm trying to add a piece of code to my script that will update its own entry in crontab. So far, I've been working with with the following:
crontab -u root -l | grep -w "$VAR" | crontab -u root - && { crontab -l -u root 2>/dev/null; echo "0 */2 * * * /root/$VAR/script > /dev/null 2>&1"; } | crontab -u root -
The objective is to have this run on several scripts and all of them will find and update its own line, based on $VAR.
For example:
script1
#!/bin/bash
VAR="home1"
crontab -u root -l | grep -w "$VAR" | crontab -u root - && { crontab -l -u root 2>/dev/null; echo "0 */2 * * * /root/$VAR/script > /dev/null 2>&1"; } | crontab -u root -
echo "Hi"
exit 0
script2
#!/bin/bash
VAR="home2"
crontab -u root -l | grep -w "$VAR" | crontab -u root - && { crontab -l -u root 2>/dev/null; echo "0 */2 * * * /root/$VAR/script > /dev/null 2>&1"; } | crontab -u root -
echo "Hi"
exit 0
What I'm trying to add is have these scripts create the following crontab:
0 */2 * * * /root/home1/script > /dev/null 2>&1
0 */2 * * * /root/home2/script > /dev/null 2>&1
For some reason the last running overwrites all entries.
Any tips? Thank you!