I used the crontab before but cannot get any command running anymore.
I am editing directly via crontab -e and testing with simple commands like
* * * * * echo "hello there" >> /Users/myUsername/Desktop/test.txt
Running this command ps -ef | grep cron | grep -v grep gives me this output:
0 270 1 0 6Sep20 ?? 0:00.61 /usr/sbin/cron
Today is 22Sep20. Did the crontab stop running?
My shell is zsh on MacOS.
On MacOS run crontab -l to list all installed cron scripts
or crontab -l -u [user] for another user.
Your * * * * * syntax means it's running every minute and that looks all fine to me. Check syntax here
Related
I'm trying to run a Docker container every other minute that is stopped via cron job but it seems not working.
What I've done is launch the command crontab -e and add the line
*/1 * * * * docker start sender >> /home/cronlog.log 2>&1
I've added the user group to Docker as explained here (in fact I can access docker from the terminal without sudo)
I have also tried to add the command into a script as below
*/1 * * * * /home/start_container.sh >> /home/cronlog.log 2>&1
with the script containing
#!/bin/sh
docker start sender
but still, nothing happens. The cron process is working tho as using the command ps -ef | grep cron I got
root 881 1 0 08:42 ? 00:00:00 /usr/sbin/cron -f -P
nicola 10905 10178 0 11:31 pts/0 00:00:00 grep --color=auto cron
Am I missing something? (Obviously, the commands work if launched manually from the terminal)
Try using the docker path instead.
type the following command to get the path of docker.
$ where docker
/usr/bin/docker
/bin/docker
then try any one of the paths in the cron script
*/1 * * * * /bin/docker start sender >> /home/cronlog.log 2>&1
or
*/1 * * * * /usr/bin/docker start sender >> /home/cronlog.log 2>&1
It turned out that, for some reason, the cron doesn't like the /home/ (at least, in this specific instance)
I've fixed using another path such as
*/1 * * * * docker start sender >> /tmp/cronlog.log 2>&1
I am trying to run the following test command every minute with cron:
apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1
This would run apachectl status and if the output contains 'requests currently being processed', it would print the result of the command date to the file output.txt.
If I run this in the terminal as the root user, there is no issue. However, running it as a cronjob every minute (I've added it to /var/spool/cron/root) as follows:
*/1 * * * * apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1
Does nothing - the output.txt file never gets generated and the job never shows in the log at /var/log/cron. What might the issue be?
You should specify the full path to the executable files, you may easily find it using which command, like which apachectl etc.
*/1 * * * * /usr/sbin/apachectl status | /bin/grep 'requests currently being processed' && 'date' >> /output.txt 2>&1
did someone use crontab on juniper swith EX series? I would like to do restart httpd job, because I need to run this process with different config.
I create some simple script with logging.
#!/bin/csh
echo 'Go...'
cp /jail/var/etc/httpd.conf_new /jail/var/etc/httpd.conf
echo 'changed'
set http_pid=`ps aux | grep 'httpd --config' | grep -v grep | awk '{print $2}'`
echo 'Process PID: ' "$http_pid"
kill -9 "$http_pid"
Also I create a job in crontab
10 12 * * * csh /var/root/test.sh >> test.log
When I run proces from cmd then output is:
Go...
changed
Process PID: 3158
And everything is ok but when I run it from cron then it looks like that:
Go...
changed
Process PID:
I try to change(add) to the crontab line with:
SHELL=/bin/csh
PATH=...
but didn't work. Also I try to change line with job to something like below:
10 12 * * * /var/root/.profile; csh /var/root/test.sh >> test.log
10 12 * * * sh /var/root/.profile; csh /var/root/test.sh >> test.log
10 12 * * * (sh /var/root/.profile; csh /var/root/test.sh) >> test.log
Maybe you know what can I do more to run this correctly?
I resolved this over the weekend. When the script is run via crontab using #reboot, the script runs before httpd is running, therefore there is no PID. I put a sleep 60 in my crontab line and the script delayed for 60 seconds, when it runs, httpd is up and running and has a PID.
Here is my Crontab:
#reboot sleep 60; sh /jail/var/etc/httpd_replace_config.sh > /jail/var/etc/httpd_replace_config.txt
I want to start pppd whenever it disconnects. I am trying to setup a shell script to run every 1 minute to see if it's down and reconnect.
I have a bash script called vpn-check.sh:
ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
pon VPNname
fi
When I run this script from cli directly, it works and starts ppp but when I run the same through cronjob (for root user), it doesn't work.
I tried the below and didn't work
*/1 * * * * bash /root/vpn-check.sh > /root/cronlog.txt 2>&1
I tried the below and didn't work
*/1 * * * * /root/vpn-check.sh > /root/cronlog.txt 2>&1
Finally, I tried:
*/1 * * * * /usr/sbin/pppd call VPNname> /root/cronlog.txt 2>&1
Can't figure out what could be wrong.
I still don't understand why some scripts work and don't work from cron when it is being called as the correct user according to the logs.
The only solution I found is to run:
crontab -e
and add the following lines to the top (even though I am calling the pppd daemon by the full path):
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I fixed it. All this while I was running crontab -e
but for the user name to be added, it needs to be added to the system-wide cron file found under /etc/crontab
user that starts the job can only be added in the above mentioned system wide cron file.
You are missing the shebang from your shell script.
vpn-check.sh should look like:
#!/bin/bash
ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
pon VPNname
fi
See:
http://linuxconfig.org/bash-scripting-tutorial
What does the line "#!/bin/sh" mean in a UNIX shell script?
I was getting the same problem too.I used #dave's answer to figure it out. You just need to add the user name to the crontab, Add the next line to the end of /etc/crontab file:
*/1 * * * * root bash /root/vpn.sh
Replace the shell name of your own.
I would like to add a crontab schedule by doing this in my server:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > crontab -e
Is there a way to do this without going doing crontab -e and then typing in the command?
Could try
1)nano /etc/crontab (or any other editor, e.g. emacs)
2)echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /etc/crontab
3)or put the contents of this into a file, then do "file > /etc/crontab"
like root:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /var/spool/cron/crontabs/username
We have the following setup in production on RHEL:
- a custom software starting sh in init.d , which
- handles cron start , stop , reload
- writes cron tasks into separate tmp file and loads this file with crontab -e
I have been only maintaining it for several months but it works like a charm ...
The proper fix is probably to use a file as specified in https://stackoverflow.com/a/4421284/377927, but it is possible to use tee to append a line to the crontab by doing e.g.:
echo "* * * * * ls" | EDITOR="tee -a" crontab -e
tee -a will append stdin to the file it gets specfied, the EDITOR variable tells crontab to use tee -a as editor.
If you have the whole crontab in a text file, you can upload a whole crontab to replace the old crontab by doing:
cat <crontab_text_file> | crontab -
This will wipe out your old crontab. Using '-' allows you to use standard input into the crontab.