Cronjob not process properly - sql

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

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

Cronie Setup in Arch Linux

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).

CasperJS and cronjob

so I have phantomJS and casperJS installed, everything is working fine, but I'm trying to add my casperJS file to cronjob (ubuntu) and I'm getting error:
/bin/sh: 1: /usr/local/bin/casperjs: not found
My crontab file:
0 */1 * * * PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs
/usr/local/bin/casperjs /usr/local/share/casper-test/test.js 2>&1
Any Ideas whats wrong?
If you want to use several commands on one line, you have to separate them with a semicolon:
0 */1 * * * PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs ; /usr/local/bin/casperjs /usr/local/share/casper-test/test.js 2>&1
Or, if you need to execute commands sequentially and only progress to next if the previous has been successful, use && operator.
For better readability you could just put those commands in a shell script and run that from cron.

create a backup of database every day using Cron. [putty]

I have this code which created a backup of my database.
pg_dump -U dbadmin -h 127.0.0.1 123telcom -f dbbackup
Now i want to create a backup every night.
Is there a way u can execute this code with crontab?
0 3 * * * pg_dump -U dbadmin -h 127.0.0.1 123telcom -f dbbackup
I'm new to putty so if anyone could help me a little that would be great.
I suspect that you have fallen foul of cron's PATH set up.
If you look in /etc/crontab, it will define a PATH for itself and you will probably have a different PATH set up for your login.
Create your script with the first 2 lines:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
where the PATH includes whatever is set up in your environment and ensure that the script is executable.
To test what is going on try this script:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
echo $PATH >> /home/yourhome/cron.txt
create an entry in /etc/crontab:
* * * * * root /home/yourhome/yourshell.sh
tell cron about the changes by using sudo crontab -e and then just save it and exit (often Ctrl O and Ctrl X if using nano editor) or I think that you can just kill the cron process and it will re-spawn.
Then check the cron.txt file to see what it is using for PATH.
PS Don't forget to remove this script from the crontab afterwards

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