Score:1

How can I find out why my crontab is not working?

in flag

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.

vn flag
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.
Cronie avatar
in flag
@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.
au flag
You need to escape the `%` characters (`...date +"\%Y.\%m.\%d \%I.\%M \%P"...`) (see [here](https://unix.stackexchange.com/questions/119450)), 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.
au flag
More troubleshooting info [here](https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working) and [here](https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it).
au flag
@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.
Score:0
it flag

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.

Cronie avatar
in flag
Sorry, I should have mentioned that this is not GUI Ubuntu, the server is terminal only. I entered the ```echo```command 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
waltinator avatar
it flag
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 avatar
it flag
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 avatar
it flag
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]
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.