Handy Utility in Linux for Logging and Question
-
So I have a few cron jobs that I run on my LAMP server, which is Ubuntu 14.04 LTS. Anyways, the issue I've had is that I capture the output of and log, just for my own record keeping purposes. My crontab for this job looks like this:
*/15 * * * * bash /home/aj/scripts/aj-update-wordpress | ts >> /var/log/aj_cron/update_wordpress.log 2>&1
So I run this every fifteen minutes, which is overkill at the moment but I'm testing something. I'm still a little new to crontab so I bash my script, and I pipe it into the command "ts". What that is for is giving each line of output a timestamp. It is a handy utility that comes packaged with the moreutils package, which you can install with this:
apt-get install moreutils
It's really nice and I thought I'd share. Sample output:
Jan 02 19:35:39 Success: WordPress is at the latest version. Jan 02 19:35:39 Success: Updated 0/0 themes. Jan 02 19:35:39 Success: Updated 0/0 plugins. Jan 02 19:35:40 Success: WordPress is at the latest version. Jan 02 19:35:41 Success: Updated 0/0 themes. Jan 02 19:35:41 Success: Updated 0/0 plugins. Jan 02 19:35:42 Success: WordPress is at the latest version. Jan 02 19:35:43 Success: Updated 0/0 themes. Jan 02 19:35:43 Success: Updated 0/0 plugins.
However, I am having one issue with the cron job. When I run the command that is in the cron job...
bash /home/aj/scripts/aj-update-wordpress | ts >> /var/log/aj_cron/update_wordpress.log 2>&1
...manually, it works fine. Great in fact! However, the cron job throws an error, which I can see in the log. I deleted the old log file before I realized I should have kept it, but first let me show you the script:
#!/bin/sh cd /var/www/builtbyart.com wp core update --allow-root wp theme update --all --allow-root wp plugin update --all --allow-root cd /var/www/literaryworksbyaj.com wp core update --allow-root wp theme update --all --allow-root wp plugin update --all --allow-root cd /var/www/thanksaj.com wp core update --allow-root wp theme update --all --allow-root wp plugin update --all --allow-root
The wp is a command which I got from here: http://wp-cli.org/
It's a way to completely manage Wordpress via the CLI. When I execute the script manually, it works great! Again, however, the cron job was seemingly unable to find or execute "wp", which is located in /usr/local/bin/. Any suggestions?
Thanks!
A.J. -
Does the user under which the cron job is running have the proper access to the directory you mention here?
-
I'm running this through crontab, which I ran while in su mode, so full root. Definitely has permissions.
-
@scottalanmiller , where are you? Come out and play!
-
@NetworkNerd said:
Does the user under which the cron job is running have the proper access to the directory you mention here?
Yes, cron runs as the user in question. Every user has access to cron.
-
@scottalanmiller said:
@NetworkNerd said:
Does the user under which the cron job is running have the proper access to the directory you mention here?
Yes, cron runs as the user in question. Every user has access to cron.
Any ideas why it doesn't work as a cron job but works when I run it manually?
-
Why does the word "bash" precede the script? The shell is all ready specified inside of the script. Remove that. You might have a pathing issue.
-
@scottalanmiller said:
Why does the word "bash" precede the script? The shell is all ready specified inside of the script. Remove that. You might have a pathing issue.
Path is fine. It now reads as such in the crontab...
* */6 * * * /home/aj/scripts/aj-update-wordpress | ts >> /var/log/aj_cron/update_wordpress.log 2>&1
-
Same issue still?
-
@scottalanmiller said:
Same issue still?
The logging doesn't appear to be working at all right now, which is weird.