Add cron job on startup from a script

16,899

Solution 1

There are a number of ways to accomplish this. See this answer for more details. Adding @reboot xbindkeys to your crontab would work, but apparently only works when the machine is rebooted not when it comes up cold.

sudo echo '@reboot xbindkeys' >> /etc/crontab

That will add the line to your cron jobs, but typically I call the script by adding it in a line to /etc/init.d/rc.local, though I like Riccardo's suggestion of adding it to ~/.gnomerc.

Solution 2

Use crontab -e to edit a user's crontab (A list of startup scripts that users have added).
Add following line at the end:

@reboot <command>

Examples:

  • @reboot my_script.sh
  • @reboot python my_script.py arg_1 arg_2

Finally use crontab -l to make sure your script was added to the list.

Share:
16,899

Related videos on Youtube

Iulius Curt
Author by

Iulius Curt

Updated on September 18, 2022

Comments

  • Iulius Curt
    Iulius Curt over 1 year

    I would like to add a command to be run at startup from within a shell script.

    For example, I run the script once and it adds the cron job, so from that time on, each time the OS starts, a specific program is started.

    More specific, I'd need xbindkeys to run on startup and this to be done by a one-time running script.

    So far I think this line would do the work:

    @reboot xbindkeys
    

    (xbindkeys is in PATH)

    How could I add this job from a script?