How do I run a PHP script from cron? - bitnami

Are my paths correct? PHP file doesn't run and no errors in apache error or syslog.
*/5 * 10 10 * bitnami /home/bitnami/stack/php/bin/php -q /home/bitnami/htdocs/mailer.php

I know this is an old post but I thought I'd share my solution for this exact same setup. I believe Bitnami's current generation of cron installation doesn't support the format "*/5". What I had to do instead is "0,5,10,15,20,25,30,35,40,45,50,55 * 10 10 * bitnami...." which gives the intended result of running every 5 minutes.

Related

Raspberry Pi / ImageMagick: Screenshot via Crontab

I'm trying to run a script via crontab on my Raspberry Pi.
I have created the script: ScreenShot.sh
The content of the file is:
#!/bin/sh
export DISPLAY=:0 && \
import -window root -resize 20% /pathtofolder/screenshot.jpg
This works fine when I run it via SSH
/home/pi/ScreenShot.sh
I have made the script executable.
I then added it to cron via sudo crontab -e
*/1 * * * * /home/pi/ScreenShot.sh
I want the script to run ever 1 minute (I'll extend this later, but for testing purposes I have it at 1 minute).
For some reason the script does not run in crontab and does not take a screenshot.
I have noticed that if I run the script via sudo:
sudo /home/pi/ScreenShot.sh
I get the following error:
No protocol specified
import.im6: unable to open X server `:0' # error/import.c/ImportImageCommand/368.
I'm assuming when Crontab runs, it runs the script as Root, which might be causing the failure.
I enabled logging on crontab and if I view the log I see the following:
Nov 6 06:26:01 IRDigitalDisplay /USR/SBIN/CRON[12634]: (root) CMD (/home/pi/ScreenShot.sh)
Nov 6 06:26:02 IRDigitalDisplay /USR/SBIN/CRON[12633]: (CRON) info (No MTA installed, discarding output
So I'm assuming something goes wrong. However it's not writing the error to the log, but rather trying to email it to me.....
My question is:
How do I get my ImageMagick script to run in crontab, take a screen shot every X minutes, and save this into a predetermined folder?
You need to add the script to the "pi" users crontab, not root's. Start the crontab edior with this command as user "pi":
crontab -e
No sudo needed.
The crontab entry has to be:
*/5 * * * * /home/pi/ScreenShot.sh

set up logrotate for a Rails app

I've been searching online for days on how to set up my server to automatically rotate the logs for a Rails app my team recently released. I've gotten myself as far as being able to run sudo logrotate -f /etc/logrotate.conf but of course, who wants to do that all the time?
The contents of the config file for the app's log (I want to add more, but don't see a need to when I can't rotate one file yet):
/path/to/app/production.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
copytruncate
}
I've verified the /etc/logrotate.conf file contains this line:
include /etc/logrotate.d
But this is the part where I'm not too sure where to go. I've found many different approaches at actually automating the process, but none seem to work. For the record, I've verified the server has the anacron command installed, but I don't know how to configure it for any process of my own. Also, root does not have a crontab on the server yet (we haven't needed it), and I'm unsure if that's better to use than /etc/crontab. In the /etc/crontab file, I've added:
15 0 * * * root cd / && run-parts --report /etc/cron.daily
but I've seen other people use
15 0 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
Is the latter a better option? Why? If so, how do I ensure it works? Again, I don't know how to set up anacron for the task at hand.
Finally, here are the previous contents of the /etc/cron.daily/logrotate file:
/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
and after some research, I replaced that with this (which I understand a bit better):
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
Can someone explain to me what the first config was doing, and which of these two options is better? I'm unsure why I have to force this process just to get it to run. Maybe /etc/crontab doesn't work like I think it does?
Is the latter a better option? Why?
With your cron command, /etc/cron.daily/* will only ever run if the computer is on at midnight (00:15). If you turn it off at night, as some people do, it would never run.
The work around this, and instead run the command when the computer starts later in the day, one can use anacron. This is obviously less useful for servers than desktops.
Of course, you don't want to use both at once, since that would run the jobs twice a day. Therefore, cron, the most brittle mechanism, will yield to anacron by only running the job if anacron is not installed.
This is what Debian and Ubuntu does by default with their test -x /usr/sbin/anacron || crontab job prefixes.
All server distros will come with logrotate correctly set up, so you shouldn't be modifying the crontab, anacrontab, or /etc/cron.daily/logrotate. The only thing you should do is add a file to /etc/logrotate.d.
Try putting this in /etc/crontab file :
-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1
It works for me.

how to create a cron job to run a ruby script?

I want to create a cron job to run a ruby script. this is what i have put in the crontab.
2 * * * * ruby /home/mark/project/script.rb >> /home/mark/cronOutput.txt
But its not running. I think there's some problem with the environment getting loaded up when the cron runs as root.
Please help.
If your ruby is in non standard paths then personally I like to wrap my ruby calls in a shell script, thereby ensuring that all the paths etc. my ruby program needs are set correctly, and schedule the script in crontab. Do something like
2 * * * * /home/mark/project/ruby_wrapper_sh >> /home/mark/cronOutput.txt 2>&1
and your /home/mark/project/ruby_wrapper_sh should read something like
#!/bin/bash
. ~mark/.bash_profile
`ruby /home/mark/project/script.rb`
If you are using RVM, you can simply do:
rvm cron setup
Reference:
https://coderwall.com/p/vhv8aw/getting-ruby-scripts-working-with-bundler-rvm-and-cron
Check whenever(https://github.com/javan/whenever) gem to use cron jobs in Rails
working on centos
in your terminal execute
# which ruby which is find your ruby path
example output
/usr/bin/ruby
Then you can edit your cronjob, using crontab -e
* * * * * /usr/bin/ruby /home/mark/project/script.rb
and save, this simply working on my centos server.
You can test the code first using this command before you edit your cronjob
#/usr/bin/ruby /home/mark/project/script.rb
it should be working first, then you can put on your crontab
Thanks for #saihgala for his solution, but I'm little modified this way.
I add #!/usr/bin/env ruby sting to the beginning of my ruby executable file.
Add permissions for this file, launch crontab file edit crontab -e.
Add */1 * * * 0-5 /path/to/executable.rb>>/path/to/output.txt and then it works for me.

Cron job can't get it to run, What syntax to use for the crontab?

I'm trying to get a rails job running with CRON. All the examples I find direct me to other rails tools, plugins, gems, etc, which is good, but I really just want to use CRON, regardless. I can run my job ok with the following, but when I've tried cron I haven't had any luck (just doesn't seem to do anything). I want to run it every 3 minutes (for testing).
/usr/bin/env ruby ~/Dropbox/98_2011/webs/apps238/swapper/script/runner /home/durrantm/Dropbox/98_2011/webs/apps238/swapper/app/controllers/scheduled_emails_controller.rb
I'm on Linux Ubuntu.
My PATH has:
/var/lib/gems/1.8/bin:/home/durrantm/.rvm/gems/ruby-1.8.7-p302/bin:/home/durrantm/.rvm/gems/ruby-1.8.7-p302#global/bin:/home/durrantm/.rvm/rubies/ruby-1.8.7-p302/bin:/home/durrantm/.rvm/bin:/home/durrantm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/pgsql/bin
Cron jobs don't load the user's environment. Try adding RAILS_ENV=production before your command within crontab, or whichever environment you need.
Example:
RAILS_ENV=production
*/3 * * * * /your/command/here
OR, if you want to make sure you have your user's full environment, execute the command within a login shell:
*/3 * * * * bash --login -c '/your/command/here'
Get rid of the home dir expansion character (replace ~ with /home/user/etc/etc) and you should be in good shape (quite likely cron's expansion of ~ doesn't match your users).
If the other parts of the syntax are bothersome there's an easy gui.
http://gnome-schedule.sourceforge.net/
sudo apt-get install gnome-schedule
You'll still have to have the path to your rb file fixed up though.
1- you might not have permissions. try running crontab -e as root
2- why don't you write to a log file to debug the issue:
*/3 * * * * /your/command/here >> /path/to/logfile

Thinking Sphinx indexing succeeding on command line, but failing in Cron job

I admit I've cobbled together a mostly working production setup on Ubuntu with Capistrano from the official docs (which seem dated and make a lot of assumptions) and various blog posts of varying outdatedness. Anyway, the last annoying hang up is that indexing works when I do it by hand (and on deploy I'm pretty sure), but doesn't work from Cron.
Here's my crontab:
$ crontab -l
# m h dom mon dow command
* * * * * cd /var/www/app/current && /usr/local/bin/rake RAILS_ENV=production thinking_sphinx:index >> /var/www/app/current/log/cron.log 2>&1
Here is the log output (this actually appears 3 times per call):
Sphinx cannot be found on your system. You may need to configure the following
settings in your config/sphinx.yml file:
* bin_path
* searchd_binary_name
* indexer_binary_name
For more information, read the documentation:
http://freelancing-god.github.com/ts/en/advanced_config.html
This is when I run the same command by hand (also works when logging):
$ cd /var/www/app/current && /usr/local/bin/rake RAILS_ENV=production thinking_sphinx:index
(in /var/www/app/releases/20100729042739)
Generating Configuration to /var/www/app/releases/20100729042739/config/production.sphinx.conf
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/var/www/app/releases/20100729042739/config/production.sphinx.conf'...
indexing index 'app_core'...
collected 5218 docs, 3.9 MB
collected 5218 attr values
sorted 0.0 Mvalues, 100.0% done
sorted 0.7 Mhits, 100.0% done
total 5218 docs, 3898744 bytes
total 0.616 sec, 6328760 bytes/sec, 8470.28 docs/sec
distributed index 'app' can not be directly indexed; skipping.
total 3 reads, 0.008 sec, 1110.2 kb/call avg, 2.6 msec/call avg
total 15 writes, 0.016 sec, 540.4 kb/call avg, 1.0 msec/call avg
rotating indices: succesfully sent SIGHUP to searchd (pid=20101).
Also relevant:
$ which rake
/usr/local/bin/rake
$ which indexer
/usr/local/bin/indexer
The error is somewhat common, but it smells funny that it works fine from the command line, I suspect something else is weird. I have 2 other mission-critical cron jobs that run rake tasks that look identical and run fine, not sure what's different about this one. Any help would be greatly appreciated!
PS-is there an authoritative deploy config for this with current Capistrano and TS versions? It seems everyone rolls their own, and the official docs seem to be as idiosyncratic as the blog posts out there.
Is the crontab owned by the same user as the one you are logged in as when you run things manually?
Since it seems like a clear PATH issue, and cron runs with a restricted PATH (i.e. not what's in your .profile), try adding this to the top of your crontab file.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
Or if you don't want to modify cron's PATH, you could symlink the files you need into /usr/sbin, which is likely in the PATH by default.
I can confirm I had a similar error like #kbighorse where the commands ran fine manually on the command line, but did not run from the cron job. I did not receive any errors, but the log file would only output the directory the sphinx command was being run from. Once I added the following path variable from #jdl to the top of the crontab file the cron job would run properly:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

Resources