0

I have a simple script and asking crontab to execute that script. In the script, I have 2 commands I want to run and append the output to a file. Only one of the commands does as it should. However, if I run the script myself as 'root', the script works. All the machines, RHEL, Ubuntu Server/Desktop and Fedora have the same outcome. Below is my script and crontab -l.

I have been researching and trying everything, even setting the $PATH, but nothing. The service httpd status will not run.

#!/bin/bash

#check httpd status and append to file
service httpd status >> /path/to/file;

#check cupsd status and append to file
netstat -punta | grep cupsd >> /path/to/file;

exit

Crontab:

*/1 * * * * /usr/local/bin/script.sh
Aaron Copley
  • 12,345
  • 5
  • 46
  • 67
chris
  • 11
  • 2
  • #!/bin/bash #check httpd status and output to file service httpd status >> /var/log/messages; #check pptpd status and append to file netstat -punta | grep cupsd >> /var/log/messages; exit – chris Jun 10 '15 at 01:34
  • personal crontab? You might (probably don't) have permission to write to /var/log/messages. – Grizly Jun 10 '15 at 01:36
  • the netstat command will write. if i run the script, it runs correctly – chris Jun 10 '15 at 01:42
  • output from 'root' running script [root@localhost ~]# /usr/local/bin/services_1.sh Redirecting to /bin/systemctl status httpd.service [root@localhost ~]# cat /var/log/messages ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead) tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1975/cupsd tcp6 0 0 ::1:631 :::* LISTEN 1975/cupsd – chris Jun 10 '15 at 01:45
  • output from crontab running - [root@localhost ~]# cat /var/log/messages ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead) tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1975/cupsd tcp6 0 0 ::1:631 :::* LISTEN 1975/cupsd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1975/cupsd tcp6 0 0 ::1:631 :::* LISTEN 1975/cup – chris Jun 10 '15 at 01:51
  • -bash /usr/local/bin/services.sh----------rw-r--r-- 1 root root 255 Aug 29 2012 /etc/crontab-----------rwxrwxrwx 1 root root 350 Jun 9 00:05 script.sh------------Shell, cron permissions, script permissions respectively – chris Jun 10 '15 at 02:07
  • 1
    `/var/log/messages` is a syslog file; you should not be overwriting this with anything. – Michael Hampton Jun 10 '15 at 02:45
  • I am not overwriting, instead appending (>>) to the file. This file is what gives me other messages through email and I would like to know some more info about my system. I understand and appreciate the concern, however, the platform this is intended for is RHEL. The output you see above was only created on my VM for testing and never had that file to begin with. – chris Jun 10 '15 at 02:49
  • I actually looked at that forum as well. I did 4 days of research on this, prior to posting the question. Only when I was able to view cronlog on my VPS, I was able to see the error message. Thanks Andrew – chris Jun 11 '15 at 02:02

1 Answers1

1

Solution:

Instead of running 'service httpd status' run '/etc/init.d/httpd status

This has worked flawlessly. What keyed me onto this was attempting to run this in Ubuntu 14.04.2 LTS, I looked at the cron log for errors, found the errors online and substituted the above.

chris
  • 11
  • 2