Run commands at startup

8,566

To execute a script at startup of Ubuntu

Edit /etc/rc.local and add your commands
The script must always end with exit 0 

To execute a script upon rebooting Ubuntu

Put your script in /etc/rc0.d
Make it executable (sudo chmod +x myscript)
Note: The scripts in this directory are executed in alphabetical order 

The name of your script must begin with K99 to run at the right time.

I'm not sure if this will help you in your situation but I will look some more and see what I can come up with.

To execute a script at shutdown

Put your script in /etc/rc6.d
Make it executable (sudo chmod +x myscript)
Note: The scripts in this directory are executed in alphabetical order 

The name of your script must begin with K99 to run at the right time.

This question has been asked before here is the link. How to run commands at login on ubuntu 12.04?

Share:
8,566

Related videos on Youtube

Sayid
Author by

Sayid

Updated on September 18, 2022

Comments

  • Sayid
    Sayid over 1 year

    My problem is as follows: I want to to modify my Ubuntu LiveCD to run several commands at startup, but I'm new to Ubuntu development and don't know how to do this. What is the proper way to run these commands every time I boot Ubuntu?

    Here are ex. commands:

    sudo rm /home/ubuntu/Desktop/examples.desktop
    sudo perl -e 'print("[DesktopEntry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=xinput_calibrator\nName=Calibraion\nExec=env /usr/bin/xinput_calibrator\n");' > /home/ubuntu/Desktop/Calibrate.desktop
    sudo /home/ubuntu/Desktop/Calibrate.desktop
    

    EDIT!!!

    I've actually tried modifying rc.local file and it looks like this now:

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    
    sudo rm /home/ubuntu/Desktop/examples.desktop
    sudo perl -e 'print("[DesktopEntry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=xinput_calibrator\nName=Calibraion\nExec=env /usr/bin/xinput_calibrator\n");' > /home/ubuntu/Desktop/Calibrate.desktop
    sudo /home/ubuntu/Desktop/Calibrate.desktop
    
    exit 0
    

    But still I don't know if it is going to work. I want to be sure before I'll build iso.

    EDIT 2!!!

    I've build iso file using this tutorial: https://help.ubuntu.com/community/LiveCDCustomization I've done steps:

    1. Extract the CD .iso contents
    2. Extract the Desktop system
    3. then I've edited the rc.local file and saved
    4. Assembling the file system ( I've ommited "Regenerate manifest" point)

    and now I get error

    No init found. Try passing init= bootarg

    while booting my LiveCD.

    passing init= bootarg results in:

    /bin/sh: bootarg: not found

    What am I doing wrong?

    Thanks

    • Web-E
      Web-E almost 12 years
      you don't need sudo in rc.local. remove them other wise it won't work
    • Sayid
      Sayid almost 12 years
      I've edited the question again
    • Admin
      Admin almost 12 years
      Would it be easier to create a live usb with persistence, so you can store your settings, and then boot off of that?
    • Sayid
      Sayid almost 12 years
      I'm sorry but it is not possible. The build must be read only.
  • Sayid
    Sayid almost 12 years
    I've edited the question
  • Doug
    Doug almost 12 years
    Bingothat is the right way to do it. Build your ISO and it should function. Let me know how it turns out.
  • Sayid
    Sayid almost 12 years
    EDITED QUESTION AGAIN