Crontab problems... 😤 [solved]
from SpongeB0B@programming.dev to linux@lemmy.ml on 28 Aug 11:32
https://programming.dev/post/36459898

Hi

When I setup a cron job like this crontab -e

*/1 * * * * echo $A_VARIABLE > /home/user/Desktop/test.txt

no problem, the file is created whether the variable exist or not.

BUT doing

*/1 * * * * cd /Path/To/Script && Script.sh
#The Script.sh
echo $A_VARIABLE > /home/user/Desktop/test.txt

Do not generate the file ! and The CRON log give me

(CRON) info (No MTA installed, discaring ouput)

and yes, the  Script.sh has the execution bit.

Any ideas ?

Thanks.

#linux

threaded - newest

I_Am_Jacks_____@sh.itjust.works on 28 Aug 11:39 next collapse

If . or /path/to/Script are not in your crontab’s PATH variable, it cannot find the script. If the script is in /path/to/Script, change your crontab to either:

*/1 * * * * cd /Path/To/Script && ./Script.sh

Or

*/1 * * * * /Path/To/Script/Script.sh

Also, “*/1” means every 1 minute and “*” means every minute, so the “/1” is unnecessary

ozoned@piefed.social on 28 Aug 11:56 collapse

This is the answer. Everyone assumes your user Crontab works just like you. It doesn't. You have to be explicit as possible, because it's basically like another person attempting to run your stuff, but not having any of your environment variables.

So you're telling it to CD, but calling the script name then tells it to look in it's path, which it won't have that. T

he ./ says 'run from right where I am right now' while '<scriptname>' says run an executable where ever it is which then immediately checks $PATH, bit not local.

So './<script name>' says run my script and it's right HERE in the place you're in right now.

Juno@lemmy.world on 28 Aug 11:41 next collapse

User permissions? I’m no pro, but when running the script it might not have the right permissions for /home/user/? Hopefully someone smarter at this can teach us both…

SpongeB0B@programming.dev on 28 Aug 12:00 next collapse

Thanks everyone,

I figured out also the missing ./ but meanwhile I got already 4 reply ! \

Thanks so much !

AnnaFrankfurter@lemmy.ml on 28 Aug 14:11 next collapse

I see you already got the answer. But if you are new to Linux I’d suggest to play overtwire bandit. This will significantly improve your Linux command knowledge.

WhatsHerBucket@lemmy.world on 28 Aug 19:49 collapse

+1 for bandit. I’m working on natas now! :)

cerement@slrpnk.net on 28 Aug 20:13 next collapse

(sidetrack: crontab guru helps you make sense of the first part of each crontab line)

diesch@loma.ml on 28 Aug 11:41 collapse

@SpongeB0B By default Linux shells don't look in the current folder for executables. Use "./Script.sh" instead of "Script.sh" to start the script.