Skip to content

Instantly share code, notes, and snippets.

@r3wt
Created September 8, 2015 17:58
Show Gist options
  • Save r3wt/0d546ce952fa969b59fe to your computer and use it in GitHub Desktop.
Save r3wt/0d546ce952fa969b59fe to your computer and use it in GitHub Desktop.
a simple way to run cronjobs on nginx/hhvm with JIT capabilities.

cron.sh:

#!/bin/bash

curl http://localhost/related.php &

curl http://localhost/artist_rank.php &

curl http://localhost/sitemap.php &

curl http://localhost/cache_validator.php &

curl http://localhost/redis_cleanup.php &

curl http://localhost/db_maintenance.php &

wait

now setup a localhost only webserver in nginx.conf to serve the crons. make sure the directory is not publically accessible from your website.

# local only webserver for cronjobs
	server {
		listen 80;
		server_name localhost;
		root /usr/share/nginx/crons;
		
		location ~ \.(php)$ {
			try_files $uri = 404;
			location ~ \..*/.*\.php$ {return 404;}
			fastcgi_split_path_info ^(.+\.php)(/.+)$;
			fastcgi_keep_conn on;
			fastcgi_pass unix:/var/run/hhvm/hhvm.sock; # ive setup a unix socket for my hhvm installation.
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
			include        fastcgi_params;
		}
		
		location / {
			index index.php;
		}
	}

now open crontab and add the command to run cron.sh!

*/10 * * * * ./usr/share/nginx/cron.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment