1

I was making a cron job to save my Minecraft world from my ram every 5 minutes. I tested the script and it seems to be working.

This is what the script looks like:

#!/bin/sh


VOLATILE="/home/jonathan/Games/Minecraft/Server/world/"
PERMANENT="/home/jonathan/Games/Minecraft/Server/world_storage/"
rsync -r -t -v "$VOLATILE" "$PERMANENT"

So then I went to add a cron job to run the script every 5 minutes, and it doesn't seem to be running it.

This is the script I used:

*/5 * * * * bash /home/jonathan/Games/Minecraft/Server/Backup.sh

Can anyone help me please?

  • cronjobs are run by root, who may have no permission to write on that folders. Does the script work if you run it as root? – jasmines Jul 16 '12 at 12:15
  • @jasmines I'm having trouble understanding your comment. Root has permissions to write everywhere, while your first sentence states otherwise. – nanofarad Jul 16 '12 at 14:05
  • You should remove the -v on that rsync command. You don't want verbose output in a cronjob; you want a cronjob to only output anything if something goes wrong. – geirha Jul 16 '12 at 19:00
  • 1
    @jasmines cronjobs are run by the user that owns the cronab (the user you do the crontab -e with) and have the same rights as this user. The environment (PATH and more) are different than the one you have at login. – Soren A Jun 21 '18 at 06:18

1 Answers1

5

You did not specify how you added your cronjob. This makes a big difference: if you've used crontab -e within your own account, scripts are run with your user (and thus the crontab entry has one field less -- the user to run it, as that is known). If you simply copied your above snippet to /etc/cron.d, it would fail as you didn't specify a user (or rather as it finds no user named "bash"). So you should take the following steps:

  1. update your question with information concerning how you added the cron job
  2. check the system logs (/var/log/syslog; they could point to possible errors)
  3. add some debug output to your Backup.sh script to see whether it is started

The third point can be achieved in multiple ways:

  • add a >>/tmp/testlog.log to the end of your crontab entry (to redirect output to a file you can investigate; additionally, a 2>&1 would include output from the error console)
  • add some lines to your script itself, like e.g. echo "Backup.sh started">/tmp/testlog.log

Moreover: As you intend your script to be run using bash, you should not tell it to use /bin/sh (which would make it use dash on a default Ubuntu installation), but rather /bin/bash. Then make it executable, and you can even omit the "bash" from your crontab entry.

Update:

According to your comment on my answer, you used crontab -e to create the job, and according to your systems logs it is executed, but the definition is rather

*/5 * * * * bash /home/jonathan/Games/Minecraft/Server/Backup.sh &>/dev/null

This redirects all output to the biggest storage in your system, the "black hole": /dev/null eats everything (but never returns anything). Redirecting STDOUT as well as STDERR this way robs you of any error report -- so you never know that they happened at all, let alone the details. For testing, you should omit the &>/dev/null part completely. Even if it works, you should only suppress unnecessary output -- as otherwise you never know when something goes wrong. So better leave out the ampersand at least, so Cron can report any occurring errors.

Furthermore: Once the output is redirected (as in your case to /dev/null), appending another redirect to the end will yield no results, as everything is already gone. So I have to adjust the above advice from "add ... to the end of your crontab entry" to "replace that in your crontab entry" ;)

Pang
  • 373
Izzy
  • 3,570
  • You're welcome. If this solved your problem, you may "accept" it (by clicking once on the checkmark on the left to it). This is a good custom on all StackExchange sites -- and helps others to go straight to the most helpful answer (well, in this case it's only one, I know). Otherwise, if you have further questions, feel free to ask them. – Izzy Jul 16 '12 at 15:56
  • Sorry i'm new to this site >_> and I kinda messed up my full comment.

    My full comment is: Thanks for the answer. I used crontab -e to make the crontab. I done what you said and I have no luck at the moment. The Syslog gives me

    " Jul 16 16:53:01 jonathan-GA-890GPA-UD3H CRON[4568]: (jonathan) CMD (sudo bash /home/jonathan/Games/Minecraft/Server/Backup.sh &>/dev/null >>/tmp/testlog.log)" every 5 minutes.

    And the testlog gives me nothing. Any ideas?

    – user77242 Jul 16 '12 at 15:57
  • The testlog cannot give you anything this way, as everything is already been eaten up by /dev/null. Change the line in your crontab to bash /home/jonathan/Games/Minecraft/Server/Backup.sh >>/tmp/testlog.log (i.e. kick off the &>/dev/null part) and try again. -- But what that log entry clearly states is that your job is executed. Which already answers your question ;) Now we just need to figure out why it is not doing what you expect -- and this I hope to see in the testlog. – Izzy Jul 16 '12 at 16:16
  • The testlog says

    " Backup.sh finished sending incremental file list ./ level.dat level.dat_old region/r.-1.0.mca region/r.0.0.mca

    sent 3749210 bytes received 94 bytes 7498608.00 bytes/sec total size is 3748911 speedup is 1.00"

    Seems to say what it says when I run the script manually.

    – user77242 Jul 16 '12 at 17:38
  • And this is my updated script ( if it helps )

    " #!/bin/sh

    echo "Backup.sh started">/tmp/testlog.log

    VOLATILE="/home/jonathan/Games/Minecraft/Server/world/"

    PERMANENT="/home/jonathan/Games/Minecraft/Server/world_storage/" echo "Backup.sh finished">/tmp/testlog.log #TODO: Check if both directories actually exist, skipped here for clearness rsync -r -t -v "$VOLATILE" "$PERMANENT" "

    – user77242 Jul 16 '12 at 17:39
  • @user77242 Glad to read it worked out! So now you can remove the debug code, and replace the redirect in your cron job by a simple >/dev/null. This way "normal output" (e.g. progress) is sent to the "black hole", but you get a note when errors occur. And as this answer solved your problem, you may "accept" it as described above -- so other users can see immediately this problem found a solution. – Izzy Jul 16 '12 at 17:45