How to Automatically run two commands after login?

31,572

Solution 1

  • Create a script file, e.g. named my_file.sh, in the /etc/profile.d/ directory.
  • Put #!/bin/bash as the first line.
  • Write whatever command(s) you want to be executed immediately after logging in, e.g. pgrep udhcpd.
  • Mark your file as executable: chmod +x /etc/profile.d/my_file.sh

*It should get executed after login. In case it doesn't or you don't have the ability to use root privileges, creating the same file under ~/.config/autostart should work fine (I haven't tried this directory before).

Solution 2

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
Share:
31,572

Related videos on Youtube

Covi
Author by

Covi

Updated on September 18, 2022

Comments

  • Covi
    Covi over 1 year

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

    autossh -M 2000 -N -f -q -D 127.0.0.1:7070 [email protected]
    

    and

    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!

    • Jo-Erlend Schinstad
      Jo-Erlend Schinstad about 12 years
      Can you update your question to explain why you're mounting manually instead of adding it to /etc/fstab?
    • Covi
      Covi about 12 years
      @Jo-ErlendSchinstad: I don't have any particular reason. fstab seems a viable option but I don't quite understand how to do it.
    • hingev
      hingev about 12 years
      add startup app with command: autossh -M 2000 -N -f -q -D 127.0.0.1:7070 [email protected] && sudo mkdir /media/C && sudo mount /dev/sda2 /media/C
  • Covi
    Covi about 12 years
    thank you! But can you point out how to achieve the first task in a non-GUI way? For the second, thank you for letting me know but I wonder if there's any disadvantage when just doing a mkdir and a mount.
  • That Brazilian Guy
    That Brazilian Guy over 11 years
    Can't find this screen on Ubuntu 12.04
  • jamesadney
    jamesadney over 11 years
    @ruda.almeida see the changes I made to the post.
  • That Brazilian Guy
    That Brazilian Guy over 11 years
    Thanks! My copy of Ubuntu is not in English, so I had a hard time finding it. Thanks for taking your time to expand this answer!
  • jamesadney
    jamesadney over 11 years
    No problem! I'm glad you got it working.
  • John Hamilton
    John Hamilton over 6 years
    Startup programs were not working for me as I lost one specific setting at every login. After adding the command to /etc/profile.d/ directory, it's now working as it should. This should definitely be picked as the answer.