How to run a bash script after 20 sec on Login?

25,321

A simple way of doing that is to add those lines to rc.local in your system.

For that you need root or sudo rights. You can edit the file with your favourite text editor, eg vim:

vim /etc/rc.local

(sleep 20
echo 'cpu limit bomi player at 40%'
cpulimit -v -e bomi -l 40) &

The first line tells the the computer to wait 20 seconds, the other 2 lines are from your script and the & at the end tells the computer to run that in a sub shell so that your computer does not wait for the function to end and will continue with boot.

You should add those lines anywhere before the exit 0 call at the end of that script since that will make it exit and ignore any lines after that.

Share:
25,321

Related videos on Youtube

Mahdi
Author by

Mahdi

I like Operating system and web security and c,c#,php programming .................... Allah will help him who moves in the way of Allah.

Updated on September 18, 2022

Comments

  • Mahdi
    Mahdi over 1 year

    I want to run a bash script like:

    #!/bin/bash          
    echo 'cpu limit bomi player at 40%'
    cpulimit -v -e bomi -l 40  
    

    Just 20 seconds after Login into my user. Can you please help me to do that? I searched Google and did what they said but it didn't work for me.

    If it's possible for the bash script to run in a new terminal window that will display the output, please tell me what I have to do to achieve that.

  • Mahdi
    Mahdi about 9 years
    thank you alot.i did that yesterday but it didnet work for me,and 20 secs i mean the ubuntu bootup and programms doing well then my script run.tnx inadv
  • Bruno Pereira
    Bruno Pereira about 9 years
    I think its because you are meant to run that as your user and not root. su user -c <command> should fix that.
  • kasperd
    kasperd about 9 years
    Wrapping the commands in ()& and using a sleep command is the right way to introduce such a delay. However I would mention, that using rc.local is only one of several ways to do it. Another possibility is to write a service script, which can then be put before or after other services as needed. It is also possible to use @reboot in a crontab.
  • Rmano
    Rmano about 9 years
    I think that the OP is confusing "boot" with "login in your desktop". See askubuntu.com/a/419185/16395
  • Bruno Pereira
    Bruno Pereira about 9 years
    @kasperd "A simple way of doing that is to add those lines to rc.local(...)"
  • Mahdi
    Mahdi about 9 years
    i want to see the new terminal window and its output that running my script after my login into ubuntu ,i already did your solution how can i do that ,there is no terminal window show up when i login and i cant see my scripts output
  • A.B.
    A.B. about 9 years
    -1 Login isn't reboot!
  • Bruno Pereira
    Bruno Pereira about 9 years
    @Mahdi then you should redirect the output to a file so you can monitor that.
  • heemayl
    heemayl about 9 years
    This is a very bad idea..user has to wait for 20 secs no matter what..see Bruno's answer to get how this can be done....
  • Mahdi
    Mahdi about 9 years
    does it mean :every time i open a new terminal this script will run ?can we use this method only for first time that we open a new terminal?(i mean dont work on every new terminal,just for th first time)
  • Rowan Hughes
    Rowan Hughes about 9 years
    Your right, the script will cause you to wait 20 seconds. simply adding '&' on the .bashrc will make it run in the background. ( i went ahead and edited my answer ). Other than that, i think this is still a viable answer
  • Rowan Hughes
    Rowan Hughes about 9 years
    @Mahdi Yes this will run everytime you open a new terminal session. Would this be a problem?
  • Mahdi
    Mahdi about 9 years
    yes , i want to run it just 1 time at the whole time when the system is running
  • EntangledLoops
    EntangledLoops almost 6 years
    This solution isn't ideal for multiple reasons. If the user quits the terminal session before 20 seconds are up, cpulimit will be killed before completing. There is no guarantee this will happen at boot. This may happen multiple times. etc.