How to use cron on a simple script


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.





Define ‘does not work’.
– Biffen
Jul 3 at 9:13





try using cron like this: * * * * * /usr/bin/sh /home/padro/Documents/script.sh
– ThisGuyHasTwoThumbs
Jul 3 at 9:13


* * * * * /usr/bin/sh /home/padro/Documents/script.sh





@Padro What makes you think you should see the output in your (or any) terminal?
– Biffen
Jul 3 at 9:21





@Biffen I don't know, it is the first time that I use cron. I suppose that if I put an echo on the script when the cron runs it the echo shows the content on terminal.
– Padro
Jul 3 at 9:28





@Padro It will print to its STDOUT, which is (most likely) not your terminal. See unix.stackexchange.com/questions/40623/…
– Biffen
Jul 3 at 9:30




2 Answers
2



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.


Hello


cd



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?