Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When I run the daily cron jobs manually, it executes perfectly and creates the database dump in the folder specified. However if I leave it overnight and let it execute on its own, it creates the file with a lock symbol on it and it's empty. Is it running the cron job as a different user or with different permissions or something? I created the cron file using sudo if that matters.
When I run the daily cron jobs manually, it executes perfectly and creates the database dump in the folder specified. However if I leave it overnight and let it execute on its own, it creates the file with a lock symbol on it and it's empty. Is it running the cron job as a different user or with different permissions or something? I created the cron file using sudo if that matters.
The first/best rule for ANY cron job is "NEVER assume your path is right"
Think about it: when you log in, you get your .bashrc/.bash_profile and the system profile executed, right? That sets your PATH, allowing you to type in such things as "mysqldump". When cron is doing it..none of those things happen; NOTHING gets inherited. So specify "/usr/bin/mysqldump" (or wherever you have it) andit may work fine. Also, you're specifying a user (cronexport) for mysqldump...does that user need a password?
Either before or after that is "Never assume your script is running as the correct user."
Indeed!
Also OP, in that vein..you can export variables and other such things in your bash shell script, so if you have something that winds up being a huge hairball (like Oracle?) where you have to set multiple environment variables and the like, you can still do it. Bang this into a script:
Code:
for i in `env | sed 's/=.*//'` ; do
unset $i
done
..and run it. THEN try to run your script manually. The above code flushes/resets EVERY environment variable you have...so if your script depends on it, it will die and you'll know what to fix.
To a cron-job always belong two parts - the cron-job and the script you'd like to execute.
Perhaps it be a good idea to post both?
Also it seems that the same one-liner did it's job already.
Quote:
When I run the daily cron jobs manually, it executes perfectly and creates the database dump in the folder specified.
Important for the proper execution of your job is a carefully set up environment for the job.
It won't work if you try to setup an environment in the shellscript because when the shellscript tries to start executing it's 'already too late' and the script got spawned by the parent process.
Put all necessary environment variables in the cronjob.
Trying to implement a 'control-instance' in form of a popping up window for failures or similar without using the c-preprocessor is a very challenging task by itself too but that's off-topic.
Question:
Did you change something in your configuration which could have lead to the faulty behaviour?
Thanks, it appears I may need to add some paths into the file. This concept is new to me, any suggestions what I should be adding to let this run? Right now I still have the file the same as it was in the original post.
Ignore cron.daily, and don't use sudo. Open a terminal as your regular user, the same one you want to run the command, and run "crontab -e". That will edit that user's personal crontab. The first 5 columns control the day/time the command is run, if you want to run at 00:00 local time every day then use "0 0 * * *". After that comes the command. Never assume your PATH is correct, always use the absolute path to any commands. The final entry should look similar to:
Ignore cron.daily, and don't use sudo. Open a terminal as your regular user, the same one you want to run the command, and run "crontab -e". That will edit that user's personal crontab. The first 5 columns control the day/time the command is run, if you want to run at 00:00 local time every day then use "0 0 * * *". After that comes the command. Never assume your PATH is correct, always use the absolute path to any commands. The final entry should look similar to:
Thanks for that. Should I be ignoring the daily system that Ubuntu has migrated to though? I was thinking if that's the way they're moving then I should embrace it, it must be better somehow.
There's nothing wrong with cron.hourly, cron.daily, cron.weekly, etc., but those are really more for system maintenance types of jobs (logrotate, mlocate, etc). They're not new, they've been around for many many years, but they're not built for user-specific jobs like this, so they're not going to replace users' personal crontabs. Different tools for different jobs.
As a general rule, if it requires root/sudo access, then you shouldn't be doing regular user tasks with it. Regular user tasks should be implemented using regular user commands, if it needs elevated privileges then you're likely doing something wrong.
Last edited by suicidaleggroll; 03-29-2017 at 01:03 PM.
Ignore cron.daily, and don't use sudo. Open a terminal as your regular user, the same one you want to run the command, and run "crontab -e". That will edit that user's personal crontab. The first 5 columns control the day/time the command is run, if you want to run at 00:00 local time every day then use "0 0 * * *". After that comes the command. Never assume your PATH is correct, always use the absolute path to any commands. The final entry should look similar to:
Cool, thanks (and to the poster above this). I implemented this, now do I need to run any other command after that? Is there some sort of push I need to do in order to make this cron job live or something? Also, how do I know where the path to mysqldump is? It's a default command line tool used with MySQL databases, it's included with them. I can run it from anywhere.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.