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.

Cite this blog post:
@article{
    ericmjl-2023-use-startup,
    author = {Eric J. Ma},
    title = {Use Cron to execute commands on startup},
    year = {2023},
    month = {08},
    day = {22},
    howpublished = {\url{https://ericmjl.github.io}},
    journal = {Eric J. Ma's Blog},
    url = {https://ericmjl.github.io/blog/2023/8/22/use-cron-to-execute-commands-on-startup},
}
  

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 teams that are looking to leverage GenAI for maximum impact. Consider booking a call on Calendly if you're interested!