Scheduled commands on a server
Some work has to happen on a timer: clearing old files, pruning exports, sending a nightly report, or running Laravel's own scheduler every minute. On a Linux server that is the job of cron. SrvYard gives you a list instead of a crontab: you add a command and a schedule, and SrvYard writes the crontab, keeps it in step with the list, captures each run's output and can warn you when a run does not happen.
The same list holds the entries SrvYard creates for you. Every Laravel site gets a Scheduler: your-domain job that runs php artisan schedule:run every minute, so you will see those next to your own jobs.
Before you begin
- A ready server. Jobs can be added, edited and run only while the server is Ready.
- A role that can manage scheduled jobs on the server. Without it you can read the list and the output, nothing more.
- A command that works on its own. Try it over SSH as the user you intend to run it as before you schedule it. Cron has a minimal environment, so use full paths where you can.
- Know the server's timezone. Times in the list and in the schedule are in the server's timezone, shown in the page description ("Times are shown in the server's timezone (UTC)."). Change it in the server's Settings card on the overview if you want local times (see Read the server overview).
1. Read the job list
Open the server and choose the Scheduler tab.
- New job. Opens the panel for a new job (step 2).
- A job. Each row shows the Job name with its command underneath, the Schedule (the frequency, the cron expression for custom schedules and Next: the next time it will run), the User it runs as, the Last run (how long ago, with ok or a failure marker) and the Status. The Laravel scheduler rows were created by the sites on this server.
- Status. Active when the job is in the crontab, Applying while a change is being written to the server, Failed if the server refused it (hover for the reason), or Paused. Search jobs filters the list and View chooses the columns.
2. Create a job
Press New job. The panel explains that SrvYard keeps the server's crontab in step with this list, and that output is saved to a log you can read.
- Name (optional). Shown in the list. Without one the command is shown instead.
- Command. One line, run by bash from the chosen user's home folder. Line breaks are replaced by spaces, so chain steps with
&&. Use full paths: for a site the application lives in/home/srvyard/your-domain/current. - Run as.
srvyard, the user your sites run as, orroot. Prefersrvyardunless the command needs to touch system files. - Frequency. One of Every minute, Hourly, Nightly (00:00), Weekly (Sunday 00:00), Monthly (1st, 00:00), On reboot or Custom.
- Cron expression. Appears for Custom. Five fields: minute, hour, day of month, month, day of week.
*/15 * * * *means every fifteen minutes. As you type, SrvYard shows the next five run times in the server's time, and an invalid expression is rejected with a message under the field. - Monitor with heartbeats. When on, the job pings a heartbeat URL after every successful run and you are notified when a ping is missed. Choose how long to wait first under Notify me after: 1, 5, 10, 15 or 30 minutes, 1 hour, 3 hours or 1 day. It is not offered for On reboot jobs. See Heartbeats and uptime checks.
Press Add job at the bottom. The job appears with the status Applying and turns Active once SrvYard has written the crontab.
Redirect nothing yourself: each job runs through a small script whose output is appended to a log file in the user's ~/.srvyard/cron folder, and that log is what Show output reads. Logs of deleted jobs are cleaned up for you.
3. Run, inspect, pause, edit or delete
Open the row menu (the three dots) of a job.
- Run now. Starts the job immediately, outside its schedule, and opens its output as it runs. Handy for testing a command exactly as cron will run it. If the server is not ready you get "Could not start the job. Check the server is ready."
- Show output. The last 20 KB of the job's log file, with a Refresh button.
- Edit. The same panel as for creating a job. The change is applied to the crontab straight away.
- Delete. Asks for confirmation (see below).
The confirmation (marked 1) says the job is removed from the server's crontab and will not run again, and that its saved output is deleted too.
Pause and Resume sit between Show output and Copy ID: a paused job stays in the list but is taken out of the crontab until you resume it. Copy ID copies the job's identifier, which is useful with the API.
A job that runs as root can change anything on the server. Only use root for work that needs it, and review commands pasted from the internet before you schedule them.
4. Things worth knowing
- Overlapping runs. Cron starts a new run on schedule even if the previous one is still going. For long jobs, wrap the command in
flock -n /tmp/job.lock your-commandso a second copy skips instead of piling up. - Laravel scheduler. One
schedule:runjob per site is enough: Laravel decides what is due inside it. Do not add a cron line for every scheduled task. The Laravel jobs are explained in Queue workers and the Laravel scheduler. - Your own crontab lines are safe. SrvYard only rewrites the block between the
# BEGIN SRVYARDand# END SRVYARDlines of each user's crontab. Lines you added by hand outside that block are kept, but they do not appear in the list. Edit SrvYard's own block only through SrvYard, since the next change overwrites it.
Troubleshooting
- The job shows Failed. Hover the status for the reason. The usual cause is an invalid expression, or the server being unreachable while SrvYard tried to write the crontab. Fix it and save again.
- It never ran. Check the status is Active (not Paused), the next run time and the server's timezone. Then use Run now: if that works, the schedule is the problem; if not, the command is.
- "Run now" fails but the command works in my terminal. Cron has no login shell. Use absolute paths for
php,nodeand your files, andcdinto the project first. - The output is empty. The command printed nothing, or it ran as another user and could not write where you expected. Run it as the same user to compare.
- I got a missed-run alert. The job did not ping within the time you chose. Check Show output for the last run and the server's load; if the job is slow, raise Notify me after.
- The cron expression is rejected. Use five fields separated by spaces. Check that ranges are valid (minutes 0 to 59, hours 0 to 23) and that you have not pasted a sixth field with seconds.
Related
- Keep long-running processes alive with daemons: for things that should run all the time instead of on a timer.
- Queue workers and the Laravel scheduler: the per-site scheduler job.
- Heartbeats and uptime checks: get alerted when a job stops running.
- Read the server overview: the scheduled jobs card and the server timezone.