date time variable into crontab -e url - ssh

i try to work with crontab -e into ssh at centos 6
i can't work with variable into crontab -e
i try many times to get it to work but i failed to run with it
MYDATE=`date`
*/1 * * * * /usr/bin/curl --head -sf http://localhost/php.php?date=$MYDATE -o /dev/null || /restart.sh
i can't work with variable into crontab -e
i try many times to get it to work but i failed to run with it
i need date into url to make refresh url every time because i use high cache i need to
test url every time with new one
there any way to run it every minute

* * * * * SUFFIX=`date +\%M`; cp /home/temp.php /home/temp3_${SUFFIX}.php >/dev/null

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

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

rsync: polling for new files

I've got:
$ rsync -azv zope#myserver:/smb/Data/*/*/* ~/rsynced_samples/
And I want it to run forever, syncing any new file as soon as it appears on myserver:
(specifying a poll interval, such as 4 seconds would be an ok comprise)
Instead of rsync you can use inotifywait which use kernel specific file changes triggers.
This script (inotify.sh) can you give an idea:
#!/bin/bash
directory=$1
inotifywait -q -m --format '%f' -e modify -e move -e create -e delete ${directory} | while read line
do
echo "doing something with: $line";
# for example:
# cp $line to <somewhere>
You can invoke this script specifying the "monitor" directory, in this way
./inotify.sh ~/Desktop/
The $line variable contains the full file path.
If you want to limit to only newly created files you can use on the flag "-e create"
Use cron to set up a check based on your time interval (say, every minute, perhaps?) . This link should help: http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
Note that a cron tab is set up on your machine side, not in your bash script
also useful: http://benr75.com/pages/using_crontab_mac_os_x_unix_linux
and here is a code example:
1) crontab -e // this opens up your current crontab or creates one if it does not exist
2) enter: * * * * * file.sh >> log.txt // this would pipe the output of your file to a log file and run it every minute.
hope that helps