Sup /g/,
I need help with a crontab entry, it seems like it should be simple, but I'm just forgetting how to get the content of a file and use it as a variable.
What I'm trying to do, is copy a text file every week on Friday, but each Friday it will be one of five different files.
eg. week 1...
cp -rf /home/user/1.txt /home/user/oncall.txt
week 2...
cp -rf /home/user/2.txt /home/user/oncall.txt
week 3...
cp -rf /home/user/3.txt /home/user/oncall.txt
and so on up to week 5.
My plan was to use something like reading the contents of /home/user/counter.txt and then if the contents of counter.txt equals 1, copy 1.txt, replace the contents of the counter.txt with the number 2, and so on up to 5 (where it would replace the number 5 with the number 1)
How should I be doing this /g/ ?
>>61093345
hey there, You can totally do this with cron, and it'd probably work fine. If I may offer an alternative, I've been doing this myself lately and it works wonderfully:
https://wiki.archlinux.org/index.php/Systemd/Timers
if you're running systemd, it can execute scripts on a schedule with .timer files that accompany .service files. even cooler, it can be even driven with .path files.
example: i have a /this/my/code directory. in this directory, is all of my application code i an writing. I would like it so that any time i drop a new file in that directory, a tool called test_my_code runs. I can make a .service file that runs test_my_code when called. and I can make a .path file that listens for any activities in /this/my/code. that way, when i drop a new file in /this/my/code my .path file is activated which activates the .service file. the .service file runs my script that does whatever :-D
>>61093345
one more not as to why this is cool. If you use systemd to manage these scheduled actions, or event driven actions, they will all be recorded with journalctl. if you need to drop this stuff to syslog somewhere, this makes it a ton easier to manage.
>>61093394
I'm on Slackware, no systemd.
I guess what I'm more wondering is if I should be doing this with some sort of a counter and a single script or if there is a way for cron to handle a "do this on Friday, then again on Friday 5 weeks from now" and run 5 different scripts.
>>61093468
i believe systemd is leveraging inotify under the hood, you might be able to accomplish something using watchdog and inotify. You could also try more than one cron job and have them overlap each other, or have the cron job edit itself x_x.
idk, 2017--cron jobs--yetch.
>>61093406
>>61093394
I'm doing this, is this something that there is anything "obviously wrong" with this that I shouldn't be doing it this way.
------------
#!/bin/bash
#Reading counter
read COUNT < /home/user/count.txt
#Week 1
if [ $COUNT = 1 ]; then
echo week 1 action
echo 2 > /home/user/count.txt
fi
#Week 2
if [ $COUNT = 2 ]; then
echo week 2 action
echo 3 > /home/user/count.txt
fi
#Week 3
if [ $COUNT = 3 ]; then
echo week 3 action
echo 4 > /home/user/count.txt
fi
#Week 4
if [ $COUNT = 4 ]; then
echo week 4 action
echo 5 > /home/user/count.txt
fi
#Week 5
if [ $COUNT = 5 ]; then
echo week 5 action
echo 1 > /home/user/count.txt
fi
>>61093554
errr, you could do a case statement instead of if/fi blocks, that way you can make a case for error handling.
>>61093830
I have actually never used case, lol.
Would you have an example?
>>61093881
sure,set -e
# Set variables
function usage()
{
echo "do stuff: "
echo " -a thing one"
echo " -b thing two"
echo " -c thing three"
exit 1
}
# Parse args
if [[ "$#" -lt 1 ]] ; then
usage
fi
while getopts "a:b:c:" opt; do
case $opt in
a)
A=$OPTARG
;;
b)
B=$OPTARG
;;
c)
C=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG"
usage
;;
esac
done
this would be good for a program that took argv. you could do$ my_prog.sh -a 1 -b 2 -c 3
the case statement then chooses what to do with those args.