Crontab on linux [closed] - linux

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't understand, I have looked at several forums to help me, etc... But it still doesn't work!
I would like to run a script thanks to Cron!
To try, I'm currently trying to execute a script that sends "test" in commands (with echo test). No problem, this script works perfectly by hand if I call the file.
But I tried to run my file with Crontab -e every minute and I've been waiting for several minutes already, but no results.
And I can't really understand why!
Already, I was told to put #! /bin/bash at the beginning of my code in my script, but when I put it in I have an error and the code doesn't execute by hand. Whereas if I don't put anything in, the code runs smoothly.
So I don't know if that's where the mistake came from, maybe.....
The final goal would be to make a script that runs every day, say to clear the cache with sync; echo 3 > /proc/sys/vm/drop_caches.
What should look like in the Crontab : 00 20 * * * PATH if I'm not mistaken.
Do you have a solution to help me?
EDIT: -bash: /root/Discord/script/cache.sh: /bin/bash^M: bad interpreter: No such file or directory it's the error I got when I runned /root/Discord/script/cache.sh to execut my script. And that command works when I don't have #! /bin/bash. But that works when I used sh cache.sh in the directory, even with #! /bin/bash !

Crontabs don't print the output on your opened terminal. You need to either create a file and then append the output there to view or test if it works. You can refer this answer https://stackoverflow.com/a/28856563/7181668
But if you want to run a shell script file through cron then you need to make sure you have given executable permission to that file and then you can use the below command in crontab -e
* * * * * /bin/sh /home/myUser/scripts/test.sh

Related

Why does my crontab not work?

I am planning to run some bash scripts every minute, and I wrote:
* * * * * bash ~/Dropbox/temp_scripts/run_all_scripts
in crontab.
It was supposed to run every minute, but it did not work. Does anyone have idea why this happens?
Transferring a comment into an answer.
Add I/O redirection to the command line in the crontab entry:
>/tmp/run_all_scripts.out 2>/tmp/run_all_scripts.err
Review the contents of the files after a minute or two has passed. Consider recording the environment to see if that's part of the problem. And consider using bash -x instead of just bash.
If you still don't get anything (the files in /tmp are not created), then you've got issues with cron; the daemon isn't running, or your user does not have permission to use it (but crontab isn't telling you that), or you've not submitted your crontab to the program (what does crontab -l say?), or … whatever is really wrong.
Note, too, that the output from cron jobs is normally (well, at least sometimes — on Mac OS X for a system I currently use, and Solaris for another that I've used previously) emailed to the person whose job it is. You should review the email on the system.
Thank you! I have already fixed it! The reason why it does not work is I used "ls -a .sh" in the script, and when the crontab did not find any *.sh files in the folder it was executing. When modifying it to "ls -a $HOME/Dropbox/temp_scripts/.sh", everything works! This debugging technique is quite helpful!
It is, in many ways, the most basic of debugging techniques — make sure you see what is actually happening. If you're not sure why a shell script isn't working, make sure you can see that it is executing and what it is producing in the way of output, and (very often) make sure you can see what it is executing with bash -x or equivalent. (AFAIK, all shells support -x to trace the execution.)

How do I make a script or program to run certain commands? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get into writing scripts that execute in the terminal. I wanted to know what the best way to do this was. I want to start by making a simple script that will run four or five commands that will update a certain program on my computer and have that run every day at a certain time. I have a programming background, but I am unfamiliar with this kind of scripting. I would appreciate any advice or input such as what language to use.
First of all, you need to open a Terminal (such as Terminal, Terminator, etc) and then you run this:
touch myScript.sh
chmod 755 myScript.sh
The first command creates an empty file and then you give 755 permissions to it. It means that it will be readable and executable by any user in your machine. If you need more details about those permissions, you can refer to the documentation here. But, believe me, those permissions will work for the moment.
Now you can insert instructions into the file using several methods: You can open it with a text editor such as vi, etc; Also, you can echo those commands this way:
echo "ls /tmp" >> myScript.sh
echo "echo 'hello'" >> myScript.sh
echo "pwd" >> myScript.sh
If you open that file, you can find that it is simply a list of commands one at each line. Then, when you run the script, each command will be executed in order from top to bottom.
You can run the script using the following sintax:
./myScript.sh
Voilá!
crontab -e
Then add following line beside the above command:
30 10 * * * script.sh
It will run everyday at 10:30 AM

How to print the output of running program started by crontab job to a file on daily basis

A script start.sh is registered in crontab to run on daily basis
But I'd like to save the output of the program to a file, like cronlog-yyyyMMdd.log
I tried the following but failed:
16 10 * * 1-5 ~/start.sh >> ~/cronlog/$(date+"\%F")-cron.log 2>&1
Anyone help me?
Using fully-qualified paths to all programs and files that are mentioned eliminate a whole class of errors from debugging issues with crontab entries. In this particular case, doing so would have only proved there were no problems with PATH issues in your crontab entry.
Your issue is that you need a space char between the date cmd and it's formatting argument string, instead of
$(date+"\%F")
#0 ^v
$(date +"\%F")
I commend you for your good crontab debug etiquette; capturing std-err and std-out to a file gives you 1. proof that your command ran when you expected it to (or if you have an error in your time specification, it will show you when the cmd did run). 2. you avoid having your localhost email box filling up with msgs from the cron daemon. 3 Any std-err msgs that were generated are also captured and are in generally in sync and time order with std-out messages.
IHTH.
16 10 * * 1-5 ~/start.sh >> ~/cronlog/$(date +"\%F")-cron.log
Edit: as explained in comments, this answers the question by correcting a typo in the asker's attempted entry

Cron says "errors in crontab file, cannot install" [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm attempting to execute the following series of commands to create backups of MySQL databases.
When I attempt to add the command to my crontab using crontab -e I get the error "errors in crontab file, cannot install" and asks me if I want to retry.
mkdir /home/mysql-backup/`date '+%m-%d-%Y'`; mysql -s -r -e 'show databases' | while read db; do mysqldump $db -r /home/mysql-backup/`date '+%m-%d-%Y'`/${db}.sql; done; rm -r -f `date --date="1 week ago" +%m-%d-%Y`; du -k |sort -n > output; mail -s "MySQL Backups" "steven#brightbear.net" < output
Is there anything I should be changing in this file? Or should I look into creating a script file and calling that from cron.
Thanks in advance for any assistance you can provide.
If you gave that script to crontab -e of course it will disagree. A line in a crontab file should start with 5 fields indicating when you want the script to run, as can be read in crontab's manpage.
On the other hand, most Linux distros nowadays have preset facilities for things that should be executed hourly (/etc/cron.hourly), daily (/etc/cron.daily), etc. It's a whole lot easier to just put your script in a file in the appropriate directory and it will get executed in the selected time raster. An added advantage is that in these files you won't be forced to cram everything into one line.
Yes; as a matter of style, if nothing else, I encourage to put the SQL commands into a shell script, and then run the shell script from cron.  (And, as Anew points out, the command sequence is easier to maintain/debug if it’s broken out into multiple lines, with comments.)  But –– is that all of what you’re feeding into crontab?  Look at man crontab and add the fields that specify when you want the command to run.
From the crontab(5) man page, it looks like percent signs (%) have a special meaning, so that is probably where you're running into trouble.
Yes, you should put your commands into a separate shell script and just call that from the crontab line. This will also make it much easier to read the crontab file, and you can format your script nicely so that it's easier to maintain. And you can test it separately from the crontab that way.

Crontab that executes shell script every minute [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have made a shell script on my red hat server that needs to be run every minute. My script is located in /media. I have edited the crontab like so:
* * * * * /media/statusshellscript.sh
My script is definitely in the location above and I know that 5 stars means run every minute.
oh.. and my script definintely works! because when I do a ./statusshellscript it works fine. Here is my script anyway, it basically just runs a php script I made which made life easier.
#!/bin/bash
# Script to execute the PHP Script
cd ~
cd /media/PHPServerTest
php -f index.php
Crontab is doing absolutely nothing at the moment. Not sure what to try next?
Also.. permissions shouldn't be a problem as i've done chmod 777 statusshellscript.
if its not running though cronjob but by command its working fine then there can be two reasons
1) you never made your file executable , that you can resolve my using the command
sudo chmod +x filename
2) your path is not correct , for finding absolute path you can use command
realpath(filename)
if realpath is not already installed it will mention you a command how to install it
by checking these points it should work fine.
The PATH for a crontab is not the same as in a shell.
Make sure that you define a PATH in your crontab that includes everything that is needed by the script.
Also, make sure that the script starts with a valid #! marker that points at the desired shell.
Or, use the full path for all commands in the script.
As others have said, my bet would be a misconfigured PATH. Try putting this into your path:
"* * * * * /media/statusshellscript.sh"
Go check that output file to see the PATH when the script is run. And rather than defining your PATH in the crontab, just define it in your script.

Resources