Cronie Setup in Arch Linux - archlinux

Here is my working crontab on debian, it runs every 5 minutes.
*/05 * * * * user /usr/bin/php /var/www/monitoring/cron/status.cron.php
I'm able to execute the script status.cron.php in my browser or php cli, the script works as intended.
Now I was forced to run the same script in Arch Linux, the thing is, Arch make use of systemd/timers and I am not used to it, then I heard about cronie and decided to give a shot, here is what I got so far:
cronie was installed and is runing, see:
● cronie.service - Periodic Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2019-12-23 00:40:31 +07; 1 months 1 days ago
Main PID: 631 (crond)
Tasks: 1 (limit: 4620)
Memory: 5.4M
CGroup: /system.slice/cronie.service
└─631 /usr/bin/crond -n
Jan 24 00:41:01 user-pc CROND[31933]: (root) CMD (run-parts /etc/cron.minutely #Runs a cron job script every minute)
created a file called monitoring in /etc/cron.d directory
here is the file:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
*/1 * * * * root run-parts /etc/cron.minutely #Runs a cron job script every minute
01 * * * * root run-parts /etc/cron.hourly #Runs a cron job script every hour
02 4 * * * root run-parts /etc/cron.daily #Runs a cron job script every day
22 4 * * 0 root run-parts /etc/cron.weekly #Runs a cron job script every week
42 4 1 * * root run-parts /etc/cron.monthly #Runs a cron job script every month
created a new directory called cron.minutely in /etc/, then created a new file called monstatuscron in it, here is the file:
#!/usr/bin/php
#*/01 * * * * user /usr/bin/php /var/www/monitoring/cron/status.cron.php
#/usr/bin/php /var/www/monitoring/cron/status.cron.php
/usr/bin/php /var/www/monitoring/cron/status.cron.php
I guess this file should be an script as it says it Arch docs, but what type of script? shell script with instruction to run my php script? or what else?
I commented the two lines right below #!/usr/bin/php cause it didn't work. I got stuck at this point.
I also run the command journalctl -xb -u cronie to check if the job is running and I got a lot of entries like this one:
Jan 24 00:58:01 user-pc CROND[32175]: (root) CMD (run-parts /etc/cron.minutely #Runs a cron job script every minute)
It seems to be running as intended.
Appreciate Any help on this one.

The shebang shouldn't be #!/usr/bin/php, but #!/usr/bin/env bash or #!/bin/bash. That is because Bash is the default shell for ArchLinux - and that is a shell script, calling a PHP script. Also make sure to chmod +x cron.minutely/*.sh, so that it can be executed. There's no need to put PHP-CLI scripts into /var/www, because they can run everywhere (there they could eventually be run via the PHP-SAPI and expose sensitive information).

Related

Crontab is not running at SSH remote server

I want to run a bash shell script every 7am every day.
But it didn't work.
This is the commands.
00 07 * * * / usr / bin / sh /home/Download/download.sh
i have tried running on local device. When I run it on a local device, the crontab works fine, but when I try on a remote server it doesn't run. any solution with this case?
I assume you want to run /home/Download/download.sh every day at 7am.
First make sure your file is executable:
chmod +x /home/Download/download.sh
Depending on your machine, the path to sh may be different. Check it with which sh command. Most times it is at /bin/sh so your crontab would look like:
0 7 * * * /bin/sh /home/Download/download.sh
But nevertheless I think you don't need /bin/sh so your crontab should look like:
0 7 * * * /home/Download/download.sh
One first
sudo crontab -e
Second
chmod +x /home/Download/download.sh
and last.
0 7 * * * /home/Download/download.sh

Cron job permission denied

I'm running a python script called TGubuntu.py.
I used ls -l , and the permissions of the script are -rwxrwxrwx 1 ubuntu ubuntu 503 Jan 13 19:07 TGubuntu.py, which should mean that anyone can execute the file, right?
But I still get in the log /bin/sh: 1: /home/ubuntu/TestTG/TGubuntu.py: Permission denied for some reason.
When I run the script manually it works perfectly.
Any Ideas?
I put it in the sudo crontab like this
* * * * * /home/ubuntu/TestTG/TGubuntu.py
But even in the root (cron) mail log it says Permission Denied!
Couldn't figure out what the problem was, so I accomplished my goal using a different method.
I ran a python script that uses the schedule module to call my script. Then I just let the "Timer" run on screen indefinitely.

Cronjob not process properly

I have created a script to gather stats on my oracle 12c database and given execute permission. Once i executed the scripts it works fine. But if i execute it through a cronjob it s not working as expected. Only the date of log file is updated and nothing in it as normal execution of the script. what would be the reason to fail the cron job
script and cron job as follows
sqlplus / as sysdba <<EOF
exec dbms_stats.gather_schema_stats('SIEBEL');
exit;
EOF
cronjob
19 12 * * * /export/home/oracle/amalw/stat_gather.sh > /export/home/oracle/amalw/stat_gather.log
db.env profile is at /export/home/oracle location
Redirect the error output to your log file with 2>&1:
19 12 * * * /export/home/oracle/amalw/stat_gather.sh \
> /export/home/oracle/amalw/stat_gather.log 2>&1
Or tell crond to mail you by adding to the top of the crontab:
MAILTO=you#gmail.com

Cronjob Homestead not working

I've created a command to send automatic emails. When I do homestead ssh and I run php artisan emails.send an email arrives in my mailtrap.io account.
I've added this code to the kernel.php
$schedule->command('emails:send')->everyFiveMinutes();
I've put it at a 5 minute interval, just to make it easier to quickly test it.
I've ssh'd into Homestead and performed
php /home/vagrant/Code/soeptime/artisan schedule:run 1>> /dev/null 2>&1
then I exit homestead and I did homestead provision
However, there is nothing in the logs and I still haven't received an email, homestead is now running for more then 15 minutes.
You need to manually edit crontab:
First:
crontab -e
and then add
* * * * * php /home/vagrant/Code/soeptime/artisan schedule:run
From the Laravel docs:
Laravel provides a convenient way to schedule Cron jobs by scheduling
a single schedule:run Artisan command to be run every minute. The
schedule:run command will examine the job schedule defined in your
App\Console\Kernel class to determine which jobs should be run.
If you would like the schedule:run command to be run for a Homestead
site, you may set the schedule option to true when defining the site:
sites:
- map: homestead.test
to: /home/vagrant/code/Laravel/public
schedule: true
The Cron job for the site will be defined in the /etc/cron.d folder of the virtual machine.

restart apache service automatically using cron 12AM daily

I have a CentOs setup in test server.
I wanna to run a cron job (the cron needs to run apache server at 12AM) daily.
My cron.daily fodler is located in /etc/cron.daily
Please let me know the steps how to implement this.
Usually I use to restart the apache service using the below command:
service httpd restart
I wanna to do restart apache service automatically using cron 12AM daily.
Thanks in advance.
While #einterview's answer is almost correct, it's important to note that a * in the minute column will run the job every minute of that hour. If intending to run once every hour, steps would be:
SSH into server.
Get list of current user's jobs with $ crontab -l
Edit jobs list with $ crontab -e (default editor will open)
Add 0 4 * * * service mysql restart for mysql at 4:00am
Add 0 5 * * * service apache2 restart for apache2 at 5:00am
Add 0 0 * * * service apache2 restart for apache2 at 12:00 am
Save and close (Ctrl+O and Ctrl+X in nano)
Recheck with $ crontab -l
I got it and give you step by step adding cron jobs into your system:
Login to your server with SSH
Type crontab -l to display list of cron jobs,
Type crontab -e to edit your crontab,
Add 0 4 * * * /etc/init.d/mysqld restart to restart Mysql everyday at 4 AM,
Add 0 5 * * * /etc/init.d/httpd restart to restart Apache everyday at 5 AM and
Add 0 24 * * * /etc/init.d/httpd restart to restart Apache everyday at 12 AM
Save your file,
Recheck with crontab -l
Get the path for service by running: which service. This should return something like /usr/sbin/service
Add entry to contrab via crontab -e and enter the following:#daily /usr/sbin/service httpd restart
If you do not want an email sent to you whenever it is run, you should instead add the following: #daily /usr/sbin/service httpd restart > /dev/null 2>&1
To find what time cron daily runs, run: grep run-parts /etc/crontab
PS: It is important to get the full path to service.
It wasn't spelled out in the other answers so I'll say it here. There is a different list of cron jobs for the current user and the root user. On my Raspberry Pi 4, doing it the way above does not work because the current user doesnt have permission to restart the service.
This works however:
sudo crontab -l (List current jobs)
sudo crontab -e (Edit cron job list)
0 0 * * * systemctl restart openvpn.service (Add this line to the bottom)
Save and close (Ctrl+O, ENTER, Ctrl+X in nano)
sudo crontab -l (Validate job was added)
In other words, "crontab -l" will give a different list than "sudo crontab -l". Adding "sudo" to the above commands makes the job run as root.
You can use following command:
crontab -e
Add following line to cron:
0 12 * * * service httpd restart
or use following command.
echo "0 12 * * * service httpd restart" | crontab -
This site is a good one for cron time https://crontab.guru
I am not allowed to comment yet on the last one here, but actually you can just use 0 0 * * * then it will go through a-ok.
Tried on ubuntu 20.04.3 LTS
sudo crontab -e
0 8 * * * /home/<user>/restart_service.sh
# Runs above crontab 8AM everyday.
Inside restart_service.sh
#!/bin/bash
systemctl restart my_service.service
Later provide appropriate permissions for execute
chmod u+x /home/<user>/restart_service.sh
following this advice adding:
0 12 * * * /etc/init.d/httpd restart
0 24 * * * /etc/init.d/httpd restart
I get "/tmp/crontab.D6cOzs/crontab":3: bad hour
errors in crontab file, can't install.
i had to do 12 only then it worked, so I'm assuming 24 is unacceptable