Are there any "cron" gurus here? I'm curious if I'm misunderstanding how cron works...
<tldr_history> I recently lost data when I rebooted a TinyCore 13.0 (x86_64) box during the update from 13.0a1 to 13.0 without making a backup first. Not like I haven't been doing this for over a decade - I guess I was just having a bad day which, of course, got worse when I realized I'd lost a couple of weeks worth of journals and script updates... and notes relating to closing out my previous job and looking for a new one. Grrr.
So I decided to finally fire up cron and have a backup made on a regular basis every couple of hours, but only if there's something new to include in the backup. I wrote a handy script that checks certain directories for new/modified files, does some logging, preserves the most recent backup, makes new backup etc etc.
</tldr_history>
My script runs great from the command line and simply uses filetool.sh -b to make the backup when needed. Nothing fancy except deciding whether or not to actually make a backup on any given run.
So I scheduled it in the crontab for user tc and it works there, too.
But when cron runs it, mydata.tgz is owned by root:root. This would seem to me to imply that the job actually runs as root:root, not as tc:staff.
So I put a line into my script, right after the backup is made, to chown tc:staff mydata.tgz. But chown failed for lack of permission. This would seem to me to imply that the job does NOT run as root:root. Indeed, the fact that cron finds the script at all (in /home/tc/bin) implies that it's running as tc, not as root.
The simple fix was to change the chown line to sudo chown tc:staff mydata.tgz, which works but begs the question "Why was mydata.tgz owned by root:root in the first place?"
The crontab entry looks like this:
0 */2 * * * autobak -c >>/tmp/autobak.log 2>&1
While the forum won't let me post the shell script in a script tag, suffice it to say there's no fiddling around with user names when invoking filetool.sh - the script just runs it in whatever the current context is.
Any ideas about the ownership of the backup file?