LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-14-2022, 02:51 PM   #1
banderas20
Member
 
Registered: Aug 2018
Posts: 138

Rep: Reputation: Disabled
Question Cron task doesn't erase directory


Hi there.

I have a php script which creates a temp directory. Then makes other stuff and finally removes the temp directory.

If I run the script with user root directly in the CLI, everything works fine.
However, the cron task throws an error while trying to delete the temp directory. Crontab is owned by root, so the user launching the script should be the same.

¿Am I missing something here?

Thanks!

PS: using Gentoo
 
Old 06-14-2022, 08:33 PM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
Quote:
If I run the script with user root directly in the CLI, everything works fine.
In your script, try using the full path to the problem directory, not the relative path. That's the most common reason why a script runs fine when run as root in a terminal but fails when run under cron. Make sure all references in your script to locations are using the full, absolute path.
 
Old 06-15-2022, 03:16 PM   #3
banderas20
Member
 
Registered: Aug 2018
Posts: 138

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kilgoretrout View Post
In your script, try using the full path to the problem directory, not the relative path. That's the most common reason why a script runs fine when run as root in a terminal but fails when run under cron. Make sure all references in your script to locations are using the full, absolute path.
I'll try and post the results. Thanks for the guidance!
 
Old 07-01-2022, 08:27 AM   #4
Lnthink
Member
 
Registered: May 2010
Location: Lafayette, LA
Distribution: Ubuntu, RH, Fedora
Posts: 44

Rep: Reputation: 11
Also, your script may be dependent on some environment variables that you use while logged in via command line.
Keep in mind that you .bashrc or any type of .profile is not run when a cron job is run. It basically has no environment.
For example is the location of a library .lib file is required, you'll have to add that environment variable to the cron job. Ditto for anything using a .so file (shared code library).
When I run something in a non-bash, non-sh command (perl, python, etc) - and it's a script/program that I don't want to mess with in its code - I may create a .sh file to call from cron which puts in all the missing environment things I want via "export" commands, and then calls the intended perl/python/etc script.
Yes, you can do all this in the cron command itself, but I really prefer clear/plain simple cron entries (pedantic, I know).
 
  


Reply

Tags
cron, cronjobs, erase



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best practice to make sure cron doesn't launch a task already launched by anacron l0f4r0 Linux - Newbie 5 09-06-2019 10:45 AM
Cron task guide and help to backup a directory islammanjurul Linux - Server 13 02-11-2016 06:49 AM
cron - how to give priority to a task running in cron.daily? MeeLee Linux - Newbie 3 11-09-2010 08:41 AM
CRON job runs, but doesn't perform the task SUSE-OES SUSE / openSUSE 2 11-20-2009 03:30 PM
[cron][mdk9.1]cron deamon seems to ignore some task... yannrichet Linux - Newbie 5 06-26-2003 09:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration