Ubuntu – How to Automatically run two commands after login?

command lineinit.dstartup

I have these two commands that I need to manually run every time after login:

autossh -M 2000 -N -f -q -D user@domain.com


sudo mkdir /media/C
sudo mount /dev/sda2 /media/C

I'd like to make them automatically run every time I boot and login to my computer. I'm currently using 10.04LTS.

BTW, I only vaguely know what init.d or runlevel mean. But I still prefer to know a command-line based way to achieve this, not a fancy GUI way. Also, the autossh will only succeed after Ubuntu automatically detect and connect to my network, so should we let it retry infinitely until successfully executed?

Thank you!

Best Answer

  • Adding the autossh Command

    1. On the panel select System --> Preferences --> Startup Applications.
    2. Under the Startup Programs tab select the Add button.
    3. Choose any Name you want and paste in your command (the Comment is optional)
    4. Click the Add button.

    Note for newer versions of Ubuntu

    You can find this application by searching for "Startup Applications" in the dash. Also, you can always launch it by pressing Alt + F2 (or opening a terminal) and running gnome-session-properties.

    Add Startup Program

    Mounting the Partition

    Option 1: Add partition to /etc/fstab

    Option 2: Use udisks

    Add this startup command as shown previously:

    udisks --mount /dev/sda2