Cron job that saves file - cron

Is it possible to run a cron job on host that also saves a file to your website directory? I have an API I download a xml file from hourly. I would like to schedule cron to automate this but I'm not sure if it's possible.

You can write a cron job to execute an arbitrary script. The script can do whatever you like provided it has the appropriate permissions.
* * * * * /bin/execute/this/script.sh
Will run the script script.sh
You need to configure the crontab entry appropriately (obviously).

Yes. You can schedule a script in cron.
In the script you can add lines to generate the file and to place the file in the location you want.
Note: Be sure that you have permissions to create a file in that specific location path.

Related

How to use cron on a simple script

I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.
This is my script (scritp.sh) which permission are 700:
#!/bin/sh
clear
echo "Hello!"
mkdir Hello
And this is the crontab file when I edit it with the command crontab -e:
SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh
EDIT:
I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can't see the echo of the script on the terminal.
EDIT2:
I have added mkdir hello, because I don't know if the echo of the script is shown on the terminal. But the hello directory is never created.
Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I'd suggest that you look in your inbox on the local machine.
To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.
Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.
The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script's working directory is, use cd in the script to move to the correct location.
I do not have enough reputation to add comment.
My humble comment would be.
Is the cron file you mentioned via root?
cos chmod 700 a file would be only be executed by owner.
If you are using redhat linux, the user account you use on the first log in is user rights NOT root.
Reference link to a cheat sheet.
su - root
system will prompt root password
crontab -e
* * * * * /home/padro/Documents/script.sh
You can even run a test script, which I did encounter the similar situation as you when I first learnt scripting into your crontab-
* * * * * date > export/home/padro/Documents/testing.txt
If you could, restart the server.
Check if your directory is correct using the command
pwd in linux/unix.
I hope my comment based on my recent learning have helped you.
Edit 1: Remove clear in your script. Thanks...
Edit 2: I believe your Hello folder is created at the core of the root folder try looking for it... or the home directory of the user...

Run script daily with cron and create compressed log file

Im trying to run PHP script on this location on every 15 minutes
0,5,10,15 * * * * /usr/local/bin/php /var/www/html/adform.php
and to put results of the script in specific folder in specific file and to compress it daily. Is it possible and how? Pls help.
This is possible.
Instead of running /usr/local/bin/php every 15 minutes, why not replace that call with a shell script which does everything you described?
This shell script would run your php script, pipe the output to a file somewhere on your system which you can then compress.

What step am I missing in creating a cron job?

I can't seem to run cron jobs and I can't figure out why. I'm new to this so I might be making an amateur mistake.
First, I create a script and call it 'test.sh', putting it in the /usr/local/bin folder. The script contains:
#!/bin/bash
echo "This test works!"
Next, I create a file called 'randomtest' in the /etc/cron.d folder. The file contains:
00 09 * * * root /usr/local/bin/test.sh >> /var/log/test.log
I expect the cron job to run at 9:00 AM every day, but for some reason, it doesn't. I also don't get a log file as expected. I checked the permissions on the test.sh file and it's currently set to 755, which should work.
Is there something I'm doing wrong? Am I missing a crucial element? Do I need to add my 'randomtest' file to the crontab or something?
Reload the cron daemon by using /etc/init.d/crond reload.
(Even if it's already running!)
The problem is that you're messing around with the /etc/cron.d directory rather than using the crontab command.
Unless you definitely need a cron job to run as root, just add it to your own crontab using the crontab command. You can use crontab -e to edit it, but it's better to keep your own copy of your crontab (ideally under version control) and use the crontab filename version of the command to install it. This ensure that the cron daemon will be aware of the update, and that any syntax errors will be caught. It also means you don't need to run any commands as root; avoiding root commands unless they're actually necessary is always a good idea.
Note that system crontabs (those in /etc/crontab and under the /etc/cron.d directory -- though those locations are implementation details that you ideally shouldn't have to worry about) have a different syntax than user crontabs; each line has an extra field that specifies the account under which the commans is to be run.
If you need a command to run as root, you can either update a system crontab file (carefully!), or you can set up a user crontab for the root user, using the normal crontab command as you would for any user account.

Trying to start a cron job on serversfree

I'm trying to add a cron job on my server, that is hosting here http://partisscan.bugs3.com/.
The provider for that is serversfree http://www.serversfree.com.
It is realy good but i can't make a cron job. I want my php file http://partisscan.bugs3.com/scan.php to be started every minute(maybe latter less often but for start). So i added a cron job in a cron job manager under control panel but it's not working.
my cron job is:
1 * * * * php -f /home/u798416153/scan.php
However it's not working:S
any ideas?
There are many possible reasons for not working.
Regarding the file:
Executing permissions of the file.
Owner of the file.
Regarding the crontab:
To which user does this crontab correspond the line you posted?
Does it have to be executed by root or any other user? If it is not root, you have to make sure that the user is not in /etc/cron.d/deny.

Where to locate Centos 6 cron job .sh file

I am really new to Linux and I apologize if this is rudimentary, but I have Google'd to find examples with no clarity and I am confused. (the question relates to a server running CentOs 6)
My questions are:
I am not sure what is the default directory that I should store a .sh file in so that a cron job can run it.
Is the syntax and sequence of my code in .sh file below correct?.
I have tested the TSQL and its fine.
#! SQL="DELETE FROM messages WHERE date < DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
MYSQL_USER="root"
MYSQL_PASS="xxxxxx"
MYSQL_DB="mydb"
I understand that the cron should contain this to do it on a daily basis:
0 0 * * *
But I am just having some apprehension of how to put it all together so I don't screw things up. A full example or explanation or a reference link would be greatly appreciated.
I believe that cron will execute the script from whichever directory it is in, given that:
the file has execution permission for the user that cron runs as (usually root if job is configured in the system-wide crontab)
the cron line specifies the full path to the script
So, if your script is /opt/script.sh, specifying this in cron:
0 0 * * * /opt/script.sh
will execute script.sh each day in 12:00am.
Please note that if this is the system-wide crontab (/etc/crontab) it should also include a username as which to execute the command:
0 0 * * * username /opt/script.sh
Also, something to make sure when working with cron is to either use full paths when calling external commands from the script or to set up the PATH variable (either in the script itself or on the crontab file). This is needed because usually the environment in which cron jobs are run is pretty restricted.
Another thing to have in mind is that if any output is generated by a cron job this output is sent via mail to the user executing the cron. So to have some feedback from the script you have to either set up the system so that the mail message ends up in a mailbox which is read by a human being or the script sends all of it's output to a log file or syslog.

Resources