• Important: We recently upgraded our forum software - please let us know if you run into any issues.

Troubles with crontab

dapeef

New Member
Hi guys, I would say I'm a very n00 n00b to linux in general and I'm currently scratching my head rather vigorously (and have been for the last 3 hours) about crontab.

Here's my setup (probably completely irrelevant but hey):
I have an old desktop PC (a box with a brain) whose sole purpose is to host 2 Minecraft servers (which it does admirably). It has no monitor or keyboard, so I use SSH from another computer.

What I'm trying to achieve:
I'm trying to use crontab to make a backup of the servers at 4:00 am everyday. It would do this by copying the parent folders (creative-server and survival-server) to a folder named with the date-stamp. It would look something like this:
Screen Shot 2019-05-20 at 18.37.37.png


The crontab file:
I installed crontab using pacman:
Code:
pacman -S cronie
When I edit the crontab file from root (everything I have done is from root), either by viewing it with
Code:
crontab -l
or editing it with
Code:
crontab -e
this is what I have:
Code:
0 4 * * * cd /home/dapeef/minecraft/1.13 && mkdir Backups/$(date "+\%Y-\%m-\%d") && cp -r current_servers/creative-server Backups/$(date "+\%Y-\%m-\%d") && cp -r current_servers/survival-server Backups/$(date "+\%Y-\%m-\%d")
The issue I'm having:
Having created that crontab file I set it to run at the next minute (eg at 18:50 I would use "51 18 * * *" to execute next minute). But it didn't run. :( I'm not sure whether it's because it didn't run at all or if it hit an error as I didn't get any feedback at all. I checked that the command worked, then tried with other commands like:
Code:
mkdir /home/dapeef/TEST
and that didn't work either.

Hopefully some infinitely more experienced person can help; I would be very grateful!

If you want me to post any other info, please let me know! (As I said I'm a n00b here; this is my first ever post to a forum :))

Thank you very much!
 


dos2unix

Member
I am a little confused. (not the first time)

Your cron tab says 0 4 * * * which would be 4am. But then you say
created that crontab file I set it to run at the next minute (eg at 18:50 I would use "51 18 * * *" to execute next minute
Are you doing this in another file?

Also just a suggestion, (not necessarily a requirement)
If you have multiple commands to run, perhaps putting it all in a shell script that runs them all would be cleaner than running multiple "&&'s"
Concurrent processes can cause timing issues. There's really no need to put cron jobs in the background, no one can see them anyway.
But for example if you try to cp the directory before mkdir finishes, it will fail.
When you use &&, the commands aren't running sequentially, they are running parallel.

Also, you didn't say which distro you are using, but on redhat/centos/fedora there is a file /var/log/cron that logs cron jobs. There is probably something similar for your distro.
 

dapeef

New Member
Your cron tab says 0 4 * * * which would be 4am. But then you say
So 0 4 * * * is when I want the command to execute when I'm done; I want it to make a backup at 4am everyday. The 51 18 * * * was just for test purposes.


If you have multiple commands to run, perhaps putting it all in a shell script that runs them all would be cleaner than running multiple "&&'s"
That's news to me; I thought they executed sequentially. Although I don't think changing this would make it work as I tried single commands too.


Also, you didn't say which distro you are using
I'm using Arch. (That was probably a silly thing to omit).

/var/log/cron
I've looked here and I can't find a cron folder. I also tried journalctl | grep cron, but I only found records of me editing the file.

Thanks for coming to my aid so soon! :)
 

Members online


Top