9

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.

8
  • 1
    Do you need to run that for your user or globally for the whole system? the solution I provided runs as root, if you want your user to be the one executing the commands change the lines after sleep with su -c user '<command>'. Apr 10, 2015 at 6:40
  • on whole system .if it was successful how can i find out that it was successful ? does a terminal come up ?
    – Mahdi
    Apr 10, 2015 at 6:50
  • Why the delay? If it is because you want to wait for dependent services to have started already, then inserting a delay is a poor workaround. If that's the situation, you should be asking how to wait for those services to get ready rather than how to wait for a specific duration.
    – kasperd
    Apr 10, 2015 at 6:59
  • well i think delay is a short way to go
    – Mahdi
    Apr 10, 2015 at 7:09
  • 1
    @Mahdi then the solution proposed should work :) Sorry for that. Apr 11, 2015 at 11:34

3 Answers 3

13

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.

8
  • 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
    – Mahdi
    Apr 10, 2015 at 6:41
  • I think its because you are meant to run that as your user and not root. su user -c <command> should fix that. Apr 10, 2015 at 6:42
  • 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.
    – kasperd
    Apr 10, 2015 at 6:55
  • does it open a new terminal that show the output of script?
    – Mahdi
    Apr 10, 2015 at 7:09
  • I think that the OP is confusing "boot" with "login in your desktop". See askubuntu.com/a/419185/16395
    – Rmano
    Apr 10, 2015 at 8:20
0

You could also enter this in the terminal:

crontab -e

and then type this in:

@reboot /path/to/script

and in the script type in at the beginning this:

sleep 20

and it might work fine this way.

0
0

Another suggestion is to create executable bash script. Lets call it onboot.sh and save it in your home directory

~/onboot.sh

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

then make it executable by running this command

chmod +x ~/onboot.sh

then add this line to your ~/.bashrc

~/onboot.sh &

Every time you create a new terminal session it will run ~/.bashrc which will then run your ~/onboot.sh file.

EDIT: Forgot to mention that this will only run for that particular user. not for all users on the system.

6
  • 2
    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....
    – heemayl
    Apr 12, 2015 at 15:43
  • 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)
    – Mahdi
    Apr 12, 2015 at 15:49
  • 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 Apr 12, 2015 at 15:50
  • @Mahdi Yes this will run everytime you open a new terminal session. Would this be a problem? Apr 12, 2015 at 15:51
  • yes , i want to run it just 1 time at the whole time when the system is running
    – Mahdi
    Apr 12, 2015 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.