What is crontab directory path? - cron

Guys i am testing if cron runs correctly in my docker debian container.
I've set up crontab
* * * * * /bin/echo "it works!" >> test.log
But i can't find this file anywhere.
I tried
* * * * * /bin/echo "it works!" >> /var/log/test.log
not luck too.
What is ">>" function's writes to path?

'>>' redirects output to a file appending the redirected output at the end.
So it should be in /var/log/test.log
Your error should be in the cron settings.
This link really helped me the beginnnig

Related

Cronjob works with regular user but not with sudo [duplicate]

I followed some other posts in stackoverflow and successfully setup cron with RVM using rvm cron setup and injected some ENV to the crontab file.
When I was troubleshooting why the dotenv gem is not working, I realised the following:
I placed my test.rb in file path /home/myuser/ruby/test.rb and had my crontab file as shown below:
* * * * * ruby /home/myuser/ruby/test.rb >> /home/myuser/ruby/output.log
and when I puts the output of the test.rb with Dir.pwd. The output states that the rb is run in the /home/myuser/ directory instead of /home/myuser/ruby directory.
While I had a hotfix by manually changing the path. But I wonder why it is the case.
By default, cron tasks of a user are executed from the user's home directory. In order to execute the script from proper directory, you have to "cd" to it.
Consider changing your crontab to:
* * * * * cd /home/myuser/ruby && ruby ./test.rb >> /home/myuser/ruby/output.log
Good luck!
According to #Pawel Dawczak who left the answer in the comment.
the solution is to rewrite the statement in crontab as
* * * * * cd /home/myuser/ruby && ruby test.rb >> /home/myuser/ruby/output.log
Thanks!

Simple--running shell script with a cronjob

Simple Question:
I want a cron job to run a script every minute. This script (script.sh) generates a .zip file of all the files in the directory.
I have written the script in a file called script.sh:
if [ -z "$(ls -A /var/www/html/convo_files)" ]; then
exit
else
zip -rj zipped.zip /var/www/html/convo_files/*
fi
Successfully creates .zip file when I run it with command ./script.sh
My crontab script is:
*/1 * * * * /var/www/html/convo_files/script.sh
Gives message in mail (which looks correct):
updating: crap.txt (deflated 89%)
updating: script.sh (deflated 36%)
Cronjob file is within same directory as script.sh btw, yet no .zip file is created with cronjob. Really not sure how to solve this dilemma.
Have you tried out the following cron job ?
*/1 * * * * /path/to/your/script.sh
This should execute your script every minute.
If you have problems executing the script make sure you have the "executable flag" set (chmod +x /path/to/your/script.sh)
If you still have errors you should take a look at your syslog. On most Linux systems the file /var/log/syslog is the currect one but this can differ from system to system.
Command in crontab file needed to be changed:
* * * * * cd /var/www/html/convo_files && /var/www/html/convo_files/script.sh
Problem was working directory wasn't set right (https://unix.stackexchange.com/questions/38951/what-is-the-working-directory-when-cron-executes-a-job )
For beginners like myself, make sure to look at your mail directory/log files to see your errors!
Note, to go into your mail directory:
cd /var/spool/mail
tail root
Thank you to everyone who answered and helped!

run script every n minutes without editing crontab file manually

Is there a command that can edit crontab ?
( I don't want to edit it manually )
For example :
command -parameter * * * * * script
The command crontab file will set the crontab to the contents of the given file - if the lines in it are properly formatted (otherwise it will just give an error).
You can use - instead of a filename to read from the standard input, e.g.:
echo "* * * * * script" | crontab -

crontab not working properly

When I run a script manually using root it is working fine. when I execute the same script inside cron it is not running. I checked the cron is actually calling the script but the script is not executing. I exported the output of echos in the script to the text file but the text file is didn't logged anything. Please check this cron
*/10 * * * * sh /var/www/sym_monitor/restart.sh > /var/www/migrate/root_restart.txt
Another approach to avoid typing full executable paths is to put shell global variables at the top of your crontab :
SHELL=/bin/sh
PATH=/bin:/usr/bin:/usr/local/bin
MAIL=me#domain.tld
*/10 * * * * stuff > log 2>&1
Note the > log 2>&1 syntax to log both STDERR & STDOUT in log file
Try changing sh for /bin/sh.
*/10 * * * * /bin/sh /var/www/sym_monitor/restart.sh > /var/www/migrate/root_restart.txt

Crontab no error but doesn't execute script

I'm trying to execute a shell script from cron on Freebsd.
To test whether crontab is working at all, I wrote the line
* * * * * echo "Hello" > /home/myuser/logile
and it work fine.
But when trying to execute any script it doesn't do anything, not even an error. (In the script I tried to run is just the same echo command)
Below is the output of crontab -l:
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
HOME=/home/myuser
MAILTO=myuser
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile
Why is the script not getting executed, although crontab is obviously running?
Permission for all files are set to rwxr-xr-x.
* * * * * /bin/sh /home/myuser/shellscript.sh
or
* * * * * /bin/bash /home/myuser/shellscript.sh
worked for me in Macosx 10.6 as rootuser
Have you checked that the command line has a linefeed/CR at the end of the line? I struggled for hours trying to find a reason for non-executing php script on cron when I simply hadn't pressed enter at the end of the line when I edited the cron jobs with crontab -e :-)
Have you checked /var/log/cron for clues?
Have you tried
* * * * * /bin/sh /home/myuser/shellscript.sh > /home/myuser/logfile
cron sends any errors via email to owner of the crontab file (often "root" so you might check that account's email). To have any errors mailed to "crontabOnFreebsd" put:
MAILTO=crontabOnFreebsd
in your crontab (near the top).
For more info issue this command:
man 5 crontab
If you are getting an error, then your logfile might not capture it, try this:
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile 2> /home/myuser/errorfile
Its been a while since I did any cron stuff; but things that always used to get me:
Environment variables not been set: generally I found it necessary to set up full paths (even to things like 'cat') to all commands [or at least set ENV variables within the script itself].
Ensure the user who owns the script etc is really the user which is running the script: this might not be the same user when you test from the interactive shell. Same goes for the directories/files that the script might write to.
Also: check the email for the root user - you might find that the errors have been diverted to the inbox, which may help you troubleshoot this further.

Resources