Command runs fine in terminal, but not when run as cron job - cron

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

Related

Cronjob stopped executing

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

The Juniper EX switch. Crontab jobs

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

Issue in crontab job

I have to write a cron job to execute a java class every after 8 hours. To test, I wrote a simple cron using :
crontab -e
*/1 * * * * echo $PATH
When I do, crontab -u <user> -l, it prints the entire cron file with above job listed. But I want to ascertain that my job is getting executed as intended. I don't see output of "echo $PATH" every minute. How can I see this output...?

Active cron job

I am trying to make a cron job for the first time but i have some problems making it work.
Here is what i have done so far:
Linux commands:
crontab -e
My cronjob looks like this:
1 * * * * wget -qO /dev/null http://mySite/myController/myView
Now when i look in:
/var/spool/cron/crontabs/
I get the following output:
marc root
if i open the file root
i see my cronjob (the one above)
However it doesnt seem like it is running.
is there a way i can check if its running or make sure that it is running?
By default cron jobs do have a log file. It should be in /var/log/syslog (depends on your system). Vouch for it and you're done. Else you can simply append the output to a log file manually by
1 * * * * wget http://mySite/myController/myView >> ~/my_log_file.txt
and see what's your output. Notice I've changed removed the quiet parameter from wget command so that there is some output.

Starting pppd from cron doesn't work

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.

Resources