1

I am a novice ... I will just get that right up front. I have a simple Ubuntu 20.04 LTS server used only as a minecraft server.

The crontab is giving me some problems

*/10 * * * * screen -S minecraft -p 0 -X stuff "save-all^M"
@reboot /home/minecraft/startserver.sh
@daily find /home/minecraft/backup/* -mtime +6 -type f -delete
@daily zip -9 -r --exclude=*backup* --exclude=*web* --exclude=*crash-reports* --exclude=*lost+found* /home/minecraft/backup/$(date +"%Y.%m.%d %I.%M %P").zip /home/minecraft

The first line works, but the @daily and @reboot do not work at all. ALL the commands work from the terminal without error.

Please help me understand this, I am not experienced with linux and I am trying to figure this out. I am aware that some people might think this would be better handled with systemd, but I am too new to figure that out yet and I don't want to install a script I can't repair.

Why is the crontab failing these lines? I don't think it is permissions. Please help me debug this

Thanks for your time

EDIT: OK, after digging around, the issue with the startserver script was within the script itself and not the crontab. I found out I need to have the -d flag for the screen command in crontab.

The zip command is working, but I can't figure out what escape code to use to add spaces in the date backup/$(date +"\%Y.\%m.\%d \%I.\%M \%P").zip Using a slash \ or using " " do not work. If anyone knows how to escape a space with creating zip files, please let me know.

I was told in the comments to use the proper format for editing the post. I don't know what that means, this is my first post.

5
  • Hi Cronie! Welcome to AskUbuntu! Is that ^M two characters (^ and M) or a carriage return in Windows format? If there's a rogue carriage return in there then the subsequent 3 lines may not be interpreted correctly. Aug 15, 2021 at 4:43
  • @tu-ReinstateMonica-dorduh That line is sending a command to the minecraft server in a screen. The ^M is in quotes, shouldn't that sequester it? --Edit: I commented out that line and it didn't solve the issue.
    – Cronie
    Aug 15, 2021 at 5:05
  • 2
    You need to escape the % characters (...date +"\%Y.\%m.\%d \%I.\%M \%P"...) (see here), and should probably also quote the wildcards in the zip command (e.g. --exclude="*backup*") (but not in the find command). Beyond that, I'd log output & errors from the commands by adding something like >>/tmp/minecraftserver.log 2>&1 to each line (but with a different filename for each one), then checking those log files to see if they give an indication what's happening. Aug 15, 2021 at 7:42
  • More troubleshooting info here and here. Aug 15, 2021 at 7:44
  • @Cronie adding spaces in the filename is tricky, because you both have to put quotes around the format string to date to tell it there's a space in the argument (rather than between arguments), and you also have to put double-quotes around the $( ) thing to tell the shell not to word-split the output it gets. It's probably cleanest to double-quote the entire filepath: ... "/home/minecraft/backup/$(date +"\%Y.\%m.\%d \%I.\%M \%P").zip" .... BTW, I second waltinator's recommendation to put this in a script (and remove those escapes), and just have the cron entry run the script. Aug 24, 2021 at 8:42

1 Answer 1

0

Jobs run through cron, or systemd startup scripts aren't run in the same runtime environment that you have on your desktop. systemd startup scripts are run as root. None of your PATH changes, or other environment variable settings are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== id ===";id;echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

4
  • Sorry, I should have mentioned that this is not GUI Ubuntu, the server is terminal only. I entered the echocommand you suggested and it gave many pages of information that I am not equipped to decipher. I was not clear on whether you meant to add #!bin/bash to the crontab or the scripts I am calling. The scripts I am running are already have #!bin/bash in their header
    – Cronie
    Aug 15, 2021 at 22:39
  • Run the echo commands in your terminal environment, saving the results in a file Rerun them via crontab, saving the results in a different file. Compare the files diff file1 file2 | less. Some environment variable(s) are missing in the the crontab-generated file, or have different values, e.g. PATH. Since the crontab command field is interpreted with /bin/sh syntax, people often mistakenly try to use more advanced `/bin/bash
    – waltinator
    Aug 15, 2021 at 23:03
  • Since the crontab command field is interpreted with /bin/sh syntax, people often mistakenly try to use more advanced /bin/bash syntax.Thus, I recommend having just a reference to the script as the crontab command (/usr/local/bin/script7), and saving the cleverness, debugging code, command generation, tests ("Is this the 2nd Tuesday?".) for the script7, which begins with #!/bin/bash.
    – waltinator
    Aug 15, 2021 at 23:15
  • Please edit your post to add new information, properly formatted. Information added via comments is hard for you to format, hard for us to read and ignored by future readers. Please click edit and add that vital information to your question so all the facts we need are in the question. Please don't use Add Comment, since that's our uplink to you. All facts about your system should go in the Question with edit
    – waltinator
    Aug 15, 2021 at 23:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .