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.

Mahdi
  • 91
  • 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>'. – Bruno Pereira Apr 10 '15 at 06: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 '15 at 06: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 '15 at 06:59
  • well i think delay is a short way to go – Mahdi Apr 10 '15 at 07:09
  • Possible duplicate of http://askubuntu.com/questions/419169/how-do-i-make-a-sudo-command-start-at-start-up-with-a-1-minute-delay (although in this case the sudo must be replaced by su -c user as per @BrunoPereira comment. – Rmano Apr 10 '15 at 08:21
  • Hey @Mahdi I think you are doing this all wrong, I just checked cpulimit, it does not limit cpu access to executables that you are not running, for that to work bomi needs to be already started and running in your system so this approach is all wrong. – Bruno Pereira Apr 10 '15 at 08:50
  • @bruno pereira: no when a programm is not in running mode then cpulimit is goning to wait for that to run:Warning: no target process found. Waiting for it... and if you use -v switch it will show process activity – Mahdi Apr 10 '15 at 14:15
  • 1
    @Mahdi then the solution proposed should work :) Sorry for that. – Bruno Pereira Apr 11 '15 at 11:34

3 Answers3

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.

Bruno Pereira
  • 73,643
  • 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 '15 at 06:41
  • I think its because you are meant to run that as your user and not root. su user -c <command> should fix that. – Bruno Pereira Apr 10 '15 at 06: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 '15 at 06:55
  • does it open a new terminal that show the output of script? – Mahdi Apr 10 '15 at 07:09
  • I think that the OP is confusing "boot" with "login in your desktop". See http://askubuntu.com/a/419185/16395 – Rmano Apr 10 '15 at 08:20
  • @kasperd "A simple way of doing that is to add those lines to rc.local(...)" – Bruno Pereira Apr 10 '15 at 08:38
  • 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 – Mahdi Apr 10 '15 at 14:09
  • @Mahdi then you should redirect the output to a file so you can monitor that. – Bruno Pereira Apr 11 '15 at 11:35
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.

A.B.
  • 90,397
Michael
  • 2,499
  • 5
  • 19
  • 24
-1

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.

  • 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 '15 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 '15 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 – Rowan Hughes Apr 12 '15 at 15:50
  • @Mahdi Yes this will run everytime you open a new terminal session. Would this be a problem? – Rowan Hughes Apr 12 '15 at 15:51
  • yes , i want to run it just 1 time at the whole time when the system is running – Mahdi Apr 12 '15 at 15:56
  • 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. – EntangledLoops May 02 '18 at 18:59