Running Multiple Commands in One Cron Job
Join commands with && to run the next only if the previous succeeded, or with ; to run each regardless of the outcome. For more than two steps, put them in a script and call that from cron — it's easier to read and debug.
Chain on success with &&
0 2 * * * cd /opt/app && git pull && npm run build
If git pull fails, npm run build is skipped — good for dependent steps.
Run each regardless with ;
0 2 * * * /opt/a.sh ; /opt/b.sh runs b even if a fails. Use this only when the steps are independent.
Prefer a wrapper script
Three or more commands get unreadable in a crontab. Put them in /opt/app/nightly.sh, add set -e at the top to stop on the first error, and schedule just the script:
0 2 * * * /opt/app/nightly.sh >> /var/log/nightly.log 2>&1
FAQ
How do I run multiple commands in one cron job?
Join them with && to stop on the first failure, or with ; to run each regardless. Example: cd /opt/app && git pull && npm run build.
What's the difference between && and ; in cron?
&& runs the next command only if the previous succeeded; ; runs the next regardless of the exit code.
How do I run many commands cleanly in cron?
Put them in a wrapper script with set -e at the top and schedule the script. It's more readable and stops on the first error.