Alternative to Cron for NearlyFreeSpeech Users

  • Posted on: 16 February 2016
  • By: davis

I use the server host NearlyFreeSpeech for all of my personal website-related things nowadays. They are extremely cheap, and their community-driven support is great.

Unfortunately, they don't have crontab enabled. Here's a workaround for enabling cron-like functionality on a NearlyFreeSpeech hosting instance.

In my case, I needed to curl multiple public-facing URL's in order to refresh some vital information. I wanted to do that on a schedule.

On my company's RackSpace server, no problem - crontab -e and we're in business.

But NearlyFreeSpeech has disabled crontab. To their credit, they have enabled their own psuedo-cron functionality with "Scheduled Tasks". Scheduled Tasks are accessible through the NFS control panel, and can be set to run every hour. But only every hour - there's no granularity on minutes. That wouldn't work for me, since I need some things to check in every minute/5 minutes.

First, we'll create a bash script called cron_every_minute.sh

#!/bin/sh
while true
do
  curl yoururlhere.com/cron/task/do_something_every_minute.php
  sleep 60 #run every minute (from when the task ends)
done

Next, we need to get this running as a daemon process

In my case, I needed to log in to the shell and run the following

[/bin]$ cd /bin
[/bin]$ dos2unix cron_every_minute.sh # I uploaded this from Windows and needed to fix the file
[/bin]$ chmod +x cron_every_minute.sh
[/bin]$ nohup ./cron_every_minute.sh 0<&- &>/dev/null &
[1] 85974 #http://stackoverflow.com/questions/3430330/best-way-to-make-a-shell-scri...

This runs the script cron_every_minute as a background daemon process.

Explanation of the nohup command:

nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout.

Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected.

-- Wikipedia

The output is redirected to /dev/null for simplicity's sake. You can redirect the logs to actual error files.

The cron_every_minute script will then run indefinitely in the background, running whatever process you fed it.

For NearlyFreeSpeech users, this may be the easiest way to get a cron-like job script running. I found solutions related to Google Cloud Engine, installed the SDK, but couldn't manage to get it to curl an outside URL. Probably will have to put more time in.

Many thanks to jdw from the NearlyFreeSpeech forums, he was very helpful in getting this running!