Score:1

Whats wrong with this cron job?

au flag
j0h

I set up a cronjob to write a webpage every 50 minutes, but when accessing them, the webpages were blank.

The cronjob: 50 * * * * mkIndex.sh > /home/user/img/index.html.

The mkIndex.sh is a script I wrote, in /home/$USER/bin, which is in the $PATH, running the script in the same way shown in crontab seems to work fine, it generates the webpage.

So why is my index.html file blank? Alternatively, can you see any flaw in my script?

#!/bin/bash
echo -e "<!DOCTYPE html>\n<head>\n"
echo -e "<style>\ndiv.gallery {\nmargin: 1px;\nborder: 1px solid #ccc;\nfloat: left;\n"
echo -e" width: 180px;\n}\n\ndiv.gallery:hover {\nborder: 1px solid #777;\n}\n\ndiv.gallery img {\n"
echo -e "width: 10%;\nheight: auto;\n}\n"
echo -e "</style>\n</head>\n"
echo -e "<body>\n"
 
cd /home/user/img
for i in `ls *.png`
do
echo -e "<div class="gallery">"
echo -e  "<a target="$i" href="$i">"
echo -e   " <img src="$i"  width="300" height="200">"
echo -e  "</a>"
echo -e "</div>"
done
 
echo -e "</body>\n</html>"

I'm not sure why my cronjob isn't generating the desired output.

in flag
Silly question, but is the cron job set up to run from `root` or from your account?
j0h avatar
au flag
j0h
omg yeah. Can a non priviliged user run cronjobs?
j0h avatar
au flag
j0h
so like this then? $crontab -e -u user 50 * * * * user mkIndex.sh > /home/user/img/index.html I'll see how that goes.
Raffa avatar
jp flag
"*Alternatively, can you see any flaw in my shell script?*" ... Yes, :-) ... Parsing the output of `ls` with `for i in $(ls *.png)` ... please see https://askubuntu.com/a/1446578/968501 for more information ... Plus the back-ticks for command substitution are deprecated in favor of `$( ... )`
Raffa avatar
jp flag
For your original question as @matigo hinted above ... You want to always use the full absolute path to your `mkIndex.sh` script in the cron entry and probably even better provide the full path to the executing shell executable file like `/bin/bash /home/user/mkIndex.sh ...`.
hr flag
`/home/$USER/bin` may well be in your PATH in an *interactive* login, but almost certainly is not in cron's PATH. See [here](https://askubuntu.com/a/919512/178692) for example.
j0h avatar
au flag
j0h
will running `# run-parts /etc/cron.hourly/` execute the task? or do I need to put a link in that folder?
Score:0
au flag
j0h

I added a link to the script in /bin where root can use it. That alone did not work, after editing ctrontab -e every way I could imagine, I deleted my entry there, and edited the file /etc/crontab to include a line for the script with root as the user.
50 * * * * root mkIndex.sh > /home/user/img/index.html

That worked.

I sit in a Tesla and translated this thread with Ai:

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.