Doesn't run this commands on mautic cron job - cron

Hi guys i have the next problem(sorry for my english):
I want to executate a command in Mautic cron job, i put the next comands:
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:segments:update
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:campaigns:update
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:campaigns:trigger
I try a lot of things but no one of them work, like:
*/1 * * * * root /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:segments:update
or
*/1 * * * * bitnami php /apps/mautic/htdocs/app/console mautic:segments:update
But i don't know what fail's,if it's the user name,the route to php,the comand,if they doesn't have permission...
If i put this manually work perfectly
php /apps/mautic/htdocs/app/console mautic:segments:update
ty so much

You should check whether the path of the php program correct. You can check the the full path of the php program in terminal output by this command:
which php
To create a cron job, should always use the full path of the program. The crontab default environment is not like the logged in user. The program may not be found if the path is not defined to the crontab environment.
The crontab syntax composed of two parts, datetime to execute & command to be executed. User is not required to add before the command.
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

I had the same problem, it turns out that my root user wasn't enabled, therefore there wasn't any way for the system to log in with a root account to perform the cronjobs.
I just made sure that I had my root account enabled with a valid pasword.
Check that by typing:
sudo passwd --status root
If this is the case, just try to change your root password
sudo passwd root

Related

Why my crontab can not work with /etc/crontab file

In my project, I want to make a scheduled task with cron.
So I added a line into /etc/crontab
*/10 * * * * root /home/JobidUserJobname/JobidUserJobname.sh
and the content of /etc/crontab is like:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/10 * * * * root /home/JobidUserJobname/JobidUserJobname.sh
*/1 * * * * root date
and I restart the crontab service:
#service crond restart
#service crond reload
and they executed successfully.
But when I executed:
#crontab -l
It shows:
no crontab for root
It seems nothing wrong. My linux OS is:
CentOS release 6.5 (Final)
Who can help me?
The crontab -l gives the output "no crontab for root". This is expected since it will show only crontabs added using the command crontab -e. The crons added in "/etc/crontab" will not be listed in crontab -l command.
Things to check.
1. Check if the file "/home/JobidUserJobname/JobidUserJobname.sh" has execute permission. If no then execute the below command.
chmod +x /home/JobidUserJobname/JobidUserJobname.sh
If its still not working append a "/bin/sh" before the script.
*/10 * * * * root /bin/sh /home/JobidUserJobname/JobidUserJobname.sh
Check the cron logs to see if there are any errors.

crontab: which one is the right job definition?

Which one is the right definition for a crontab job?
With or without the user before the execution path?
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * <user> <command>
On Debian, crontab -l show a backup example as:
....
For example, you can run a backup of all your user accounts
at 5 a.m every week with:
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
...
There is no user here!!
The /etc/crontab content (on the same box), gives a different clue:
....
and files in /etc/cron.d. These files also have username fields,
that none of the other crontabs do.
...
crontab does not * allow for specifying a user to run as...
... unless you're in the root crontab.
If you check my favorite linux admin reference, you'll not near the bottom that there are some tricks to running certain chrontab entries as a particular user. However, the best practice, if you wish to do so, would be to edit the crontab of the user:
crontab -u <username> -e
If you must...
0 0 * * * sudo -u [user] [command]
But this can only be done in the crontab of a user with sudo permissions, and as fcm pointed out, such a user could just edit the root crontab.
Most flavors of 'NIX require a user in the root crontab /etc/crontab
0 0 * * * [user] [command]
Conclusion
If you want to specify which user is running a specific cron job, the best practice is to do one of the following, depending on the use-case:
root crontab
/etc/crontab
sudo crontab
<time> <user> <command>
user crontab
crontab -u <username> -e
<time> <command>

Running a cron job at 2:30 AM everyday

How to configure a cron job to run every night at 2:30? I know how to make it run at 2, but not 2:30.
crontab -e
add:
30 2 * * * /your/command
To edit:
crontab -e
Add this command line:
30 2 * * * /your/command
Crontab Format:
MIN HOUR DOM MON DOW CMD
Format Meanings and Allowed Value:
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.
Restart cron with latest data:
service crond restart
As seen in the other answers, the syntax to use is:
30 2 * * * /your/command
# ^ ^
# | hour
# minute
Following the crontab standard format:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
It is also useful to use crontab.guru to check crontab expressions.
The expressions are added into crontab using crontab -e. Once you are done, save and exit (if you are using vi, typing :x does it). The good think of using this tool is that if you write an invalid command you are likely to get a message prompt on the form:
$ crontab -e
crontab: installing new crontab
"/tmp/crontab.tNt1NL/crontab":7: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? (y/n)
If you have further problems with crontab not running you can check Debugging crontab or Why is crontab not executing my PHP script?.
An easy way to write cron is to use the online cron generator
It will generate the line for you. One thing to note is that if you wish to run it each day (not just weekdays) you need to highlight all the days.
As an addition to the all above mentioned great answers, check the https://crontab.guru/ - a useful online resource for checking your crontab syntax.
What you get is human readable representation of what you have specified.
See the examples below:
30 2 * * * (answer of this question)
#daily
59 23 31 12 *
30 2 * * * wget https://www.yoursite.com/your_function_name
The first part is for setting cron job and the next part to call your function.
30 2 * * * Every Day at 2:30 Am
30-31 2 * * * Every Day at 2:30 -31 am
Along with he answers its important to understand the cron expressions , i face a lot of difficulty in understanding .
But an intuitive way to understand is given here .

Enable/Disable tasks in Crontab by Bash/Shell

Is there a way to enable and disable Crontab tasks using Bash/Shell?
So when the user starts Server 1, it will enable the Server 1 Crontab line and so on.
And when the user stops Server 1, the Server 1 Crontab line get disabled (#).
Is this possible and how?
Thanks in advance
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
SERVERNUM=$1
To enable:
crontab -l | sed "/^#.*Server $SERVERNUM check/s/^#//" | crontab -
To disable:
crontab -l | sed "/^[^#].*Server $SERVERNUM check/s/^/#/" | crontab -
Transcript:
barmar#dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar#dev$ crontab -l | sed '/^[^#].*Server 1 check/s/^/#/' | crontab -
barmar#dev$ crontab -l
#*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar#dev$ crontab -l | sed '/^#.*Server 1 check/s/^#//' | crontab -
barmar#dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
I suggest you add your cron jobs to /etc/cron.d for every server one script. Then let the cron script scan for some marker file if the cron job should be executed.
As a quick and dirty fix, you can enable or disable the execute permission of the appropriate cron script.
E.g. if you like to prevent locate from automatically updating its database (which can be I/O consuming):
cd /etc/cron.daily
sudo chmod a-x locate
This may be against the cron framework, but it is quickly applied and it works in case of immediate needs.
this is a variant, I use a cronjob that loads it self every night. I just edit a file and it gets reloaded at 10pm everynight. You could make the reload happen more often. I keep a directory of files for each of nodes. The trick is make sure that nobody comments out the reload line.
0 22 * * * crontab /home/ME/cron_files/NODE

Crontab is not working on Amazon EC2 server

Crontab is not working on Amazon EC2 Linux Server.
I have saved below codes in /etc/crontab file
crontab
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web
I have started crontab command already, but this one didn't work.
I also have saved this line in "crontab -e" too, but cron won't work.
* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web
Is there anyone who had same experience like me?
Thank you.
I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user. For example:
0 12 * * * python3 example.py
In fact, specifying a user here prevented it from running.
Solved the problem.
I used this code and it works!
* 2 * * * root tar cvfpz /home/backup/web_`date +\%Y\%m\%d`.tar.gz /home/web
You should use crontab -e to create cron for the logged user, so that you don't need to inform the username.
See here: https://stackoverflow.com/a/16986464/1777152
For people who are dealing with AWS machines and EBS you need to specify the root keyword before the command since ec2-user isn't allowed to run crontabs. Of course there's a way to fix that.
you can edit the crontab by typing sudo nano /etc/cron.d/mycrontabs or crontab -e
* * * * * root bla bla
Also make sure e that the file is ended with a new line
Don't use nano, use the native sudo crontab -e command.

Resources