How to sync crontab with DirectAdmin backwards? - cron

I have a few hundred cronjobs in DirectAdmin.
I need to string_replace every cronjob in DirectAdmin.
I managed to change all my cronjobs with crontab -e, so my cronfile in /var/spool/cron is now correct.
The problem is that in DirectAdmin only the old cronjobs reside.
My question is how to sync the string-replaced crons into DirectAdmin ?
So is there a way to read the changed /var/spool/cron-file into DirectAdmin ?

You better not use crontab -u userfoo -e to change your crons directly, because DirectAdmin will be unchanged.
This is better:
Step1: Open /usr/local/directadmin/data/users/userfoo/crontab.conf and add “0=”, “1=”, “2=”, etc. before every line: (don't use this before a comment line):
1=1 * * * * /usr/local/bin/php /home/userfoo/domains/www.domfoo.nl/public_html/foo1.php
2=* 2 * * * /usr/local/bin/php /home/userfoo/domains/www.domfoo.nl/public_html/foo2.php
Step2: Go to DirectAdmin, cronjobs and click right-below on Save (This will copy the crons to /var/spool/cron/)
All your crons will be active on-the-fly.

Related

Crontab how to disable emails

I want to disable email reports on some tasks which run frequently. I've gone through the following links
https://unix.stackexchange.com/questions/84335/stop-cron-sending-mail-for-backup-script
https://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command/
They suggest adding >/dev/null 2>&1 at the end of the command to disable emails.
This is my crontab entry :
* * * * * /bin/bash /home/ubuntu/startup/monitor-mosquitto.sh >/dev/null 2>&1
But I'm still receiving emails every time the script is run. In fact, not just once but I get like 8-15 mails every time it runs.
Am I doing anything wrong here ? BTW, I'm using crontab as root ( sudo crontab -e )
See man 5 crontab:
If MAILTO is defined but empty (MAILTO=""), no mail will be sent.
If memory serves, I have used that on the line itself, or preceding it:
MAILTO=""
* * * * * /bin/bash /home/ubuntu/startup/monitor-mosquitto.sh
Note that this will affect all lines that follow it so you may want to place it last, or renable MAILTO.
Also, strictly speaking, you should be able to work out what you did with shell redirection in the shell itself. What you have looks correct so I am a little puzzled. Maybe make sure to test it as root not as you.

Simple cron with flock not working on Ubuntu

I've created the file crontester on /etc/cron.d with this line on it:
* * * * * /usr/bin/flock -n /tmp/fcj.lockfile touch /tmp/test.txt
That should be running every minute.
But I dont see the /tmp/test.txt file being created, so the cron is not working correctly.
What I'm doing wrong? Do I've to create the /tmp/fcj.lockfile, if I've to do this, do I have to create it empty?
Thanks a lot.
The command runs fine on my machine, so the cronjob is probably not set up properly. man cron discourages creating /etc/cron.d/ files:
Like /etc/crontab, the files in the /etc/cron.d directory are monitored for changes. In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab.
Try creating the cronjob using crontab -e and see if it works

Can't run pg_dump from cron job

Our system is on AWS. Debian EC2 instance. RDS postgres instance.
when I log into the "admin" account pd_dump works.
$ whom
admin
$pd_dump
[it works...]
then I crontab -e in order to create a test backup event.
*/2 * * * * /home/admin/storage/db_backup.sh
and indeed, it runs every 2 minutes.
in db_backup.sh I have:
pg_dump > dbbackup.txt
the script and the folder I am running at belong to "admin".
The dbbackup.txt is always generated with a zero size.
why is that?
I found out what the culprit was. Since I'm ran in the context of admin but in cron, my environment strings were not set.
setting this at the head of my db_backup.sh resolved this:
export PGPASSWORD=<value>
export PGHOST=<value>
export PGDATABASE=<value>
export PGUSER=<value>
We can only guess here, but I have a suggestion. Try redirecting ALL output (including stderr) to the file to see if it gives you some information.
in your /home/admin/storage/db_backup.sh add 2>&1:
pg_dump > dbbackup.txt 2>&1
you may also use this and redirect your cron line in case your .sh file has some problems:
*/2 * * * * /home/admin/storage/db_backup.sh >> /home/admin/cron_log.txt 2>&1
EDIT: just noticed you post an answer already. Buy if you'll have time, try removing exports and see if you can get some errors in logs that may help.
According to my mac, the zero dump files were caused because crontab couldn't understand pg_dump.
Instead, I did the following. FYI I use connection string URI.
But what's important is the full path I set to the pg_dump
/Library/PostgreSQL/13/bin/pg_dump --dbname=postgresql://admin:D%40t%40sc13nc3#localhost:5432/digitaldairy | gzip > test1232323.gz

Running a crontab every 15 minutes not working on linux redhat

I want to run a crontab every 15minutes. I tried this:
0 */15 * * * ./usr/My_PATH/run.sh
But I get this error:
0 command not found
Is there something wrong with the syntax ?
Many thanks.
UPDATE:
I corrected the script and I tried this:
*/15 * * * * /My_Path/run.sh
and this
0,15,30,45 * * * * /My_Path/run.sh
In both cases I get an error.
#1 bash: */15: No such file or directory
#2 bash: 0,15,30,45 command not found
If this:
0 */15 * * * ./usr/My_PATH/run.sh
fails with this error:
0 command not found
then you're trying to run it as a shell command. You need to feed it to the crontab command. There are several ways to do this.
crontab -l will list the current contents of your crontab; it doesn't modify it.
crontab -e will open (a copy of) your crontab in a text editor and let you modify it. This is probably the simplest way to update it.
crontab filename reads the specified file and replaces your current crontab with its contents. (If you already have a crontab, this will quietly clobber it.)
The method I recommend is to keep a separate file containing your crontab (say, crontab.txt).
First, if you already have a non-empty crontab (check with crontab -l), save it to the file:
crontab -l > crontab.txt
Make whatever additions or other changes you want to that file, and then use
crontab crontab.txt
to install the updated crontab.
You can keep backup copies (I maintain mine in a source control system) so you can recover if you mess something up. And you can do a quick crontab -e if you want to test something, then re-run crontab crontab.txt to revert to the stored crontab.
The syntax of the crontab line in your question:
0 */15 * * * ./usr/My_PATH/run.sh
is correct, but the path ./usr/My_PATH/run.sh looks like it may be incorrect. Cron jobs run from your home directory, so the path is valid only if the usr directory is directly under your home directory (and in that case the ./ is unnecessary). It's probably better to specify the full path, which can start with $HOME/.
Yes.
First field is minutes. Second field is hours. You're setting it off at zero minutes past the hour, every 15th hour. So basically - 15:00 each day.
You want:
*/15 * * * * /some_script
Furthermore - ./ - it's a relative path, and that's probably a bad idea with cron, because it doesn't chdir to run stuff. Use an absolute path to avoid confusion. If you absolutely need to be in a particular directory for the script to work, you can try:
cd /path/to/script && ./this_script
So it's quite possible that you've got broken permissions or just not finding a relative path that you're using.

running cron.php in moodle

I am using a plugin in moodle that requires running cron. I manually run localhost/moodle/admin/cron.php and it worked. So my question is how can I run this script all the time and not manually. I read about C panel but I'm not sure how to use it.
Any advise is appreciated. Thanks in advance.
If you go to your cpanel on your host, then go to the cron options. It should also you how often you want the cron to run and a script to run.
The script is
usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null
If you choose to run every 15 minutes then you should see something like this after setting up cron.
*/15 * * * * /usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null
http://docs.moodle.org/25/en/Cron

Resources