Eric J Ma's Website

Use Cron to execute commands on startup

written by Eric J. Ma on 2023-08-22 | tags: automation command execution cron cron jobs init.d linux linux commands linux startup linux tutorial rc.local startup scripts systemd til


Today I learned how to execute arbitrary commands on startup on a Linux machine.

This trick uses cron, and is essentially adding something like this to your crontab:

@reboot /absolute/path/to/your/command with args

There are other ways of doing so as well, according to tutorialspoint, including using rc.local, init.d, and systemd. While I'm quite sure there are good reasons for more than one way to accomplish this task, I think using cron is pretty elegant for the following reasons:

  1. The syntax is very clean.
  2. cron is almost universally installed on Linux variants.
  3. cron is a conceptually well-accepted place for automated execution of commands on a schedule.

I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!

Finally, I do free 30-minute GenAI strategy calls for organizations who are seeking guidance on how to best leverage this technology. Consider booking a call on Calendly if you're interested!