You are not logged in.

#1 2018-05-29 03:51:25

ahura
Member
Registered: 2016-02-20
Posts: 57

[SOLVED] Weird behavior of functions defined in bashrc?

I'm running a version of Mendeley (citation manager) which has a bug that causes it to constantly use 100% of one of my CPUs.
To get around this, I went to define a command to cpu-throttle it when run, using cpulimit .
In order to allow menderley some time to syncronize files before throttling, I run

mendeleydesktop & (sleep 20; cpulimit -i -l 10 -p $!)

in a terminal. As expected, this launches Mendeley and throttles cpu usages to 10% after 30 seconds (mendeley's PID is passed to cpulimit with $!).

To automate this, I add

mendelazy(){
	mendeleydesktop & (sleep 20; cpulimit -i -l 10 -p $!) &>/dev/null  
}

to .bashrc, reload it with source .bashrc, and modify /home/ahura/.local/share/applications/mendeleydesktop.desktop so the newly defined command is instead called, by changing the Exec line into Exec=mendelazy.

However, while running mendelazy in a terminal produces the expected result, running mendeley from the 'start' menu (using Cinnamon), cpu usage remains at 100%.

If anyone can help me understand why this is, I'd be very grateful.

To be clear I AM NOT looking for information on how to downgrade mendeley or fix the original bug - I am curious about the behavior described above and wish to understand it.

Last edited by ahura (2018-05-29 16:19:52)

Offline

#2 2018-05-29 04:57:55

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,238
Website

Re: [SOLVED] Weird behavior of functions defined in bashrc?

The round brackets execute the `sleep` and `cpulimit` commands in a subshell -- I'm guessing there's different behaviour between subshells are their parents in an interactive shell vs non-interactive.  There should be no need to use the subshell in your function; try this:

mendelazy(){
  mendeleydesktop &
  sleep 20
  cpulimit -i -l 10 -p $(pidof mendeleydesktop)
}

Offline

#3 2018-05-29 15:07:08

ahura
Member
Registered: 2016-02-20
Posts: 57

Re: [SOLVED] Weird behavior of functions defined in bashrc?

It tried doing that (except using $(pidof /opt/mendeleydesktop/lib/mendeleydesktop/libexec/mendeleydesktop.x86_64) as that is the misbehaving process).

Again, it works as expected when launched in a terminal, but using the menu shortcut doesn't throttle.

Offline

#4 2018-05-29 15:11:44

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: [SOLVED] Weird behavior of functions defined in bashrc?

ahura wrote:

Again, it works as expected when launched in a terminal, but using the menu shortcut doesn't throttle.

Does the function work when run from a terminal, or do you mean only manually entering each of those commands works?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2018-05-29 15:13:42

ahura
Member
Registered: 2016-02-20
Posts: 57

Re: [SOLVED] Weird behavior of functions defined in bashrc?

Trilby wrote:

Does the function work when run from a terminal, or do you mean only manually entering each of those commands works?

Yes, simply typing mendelazy in a terminal after source .bashrc produces the expected result.

Offline

#6 2018-05-29 15:20:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: [SOLVED] Weird behavior of functions defined in bashrc?

Ok, then what's in the menu shortcut that doesn't work?  How does it not work?  Are you sure it runs the function at all?  Does it start an interactive shell (to even have the function defined)?

I just reread your first post, and changing the Exec line of a desktop file to the name of a shell function will most certainly not work ... at all.  So I gather that desktop file is not even being used by your menu/launcher.  You either need to put the content of that function in a script called mendelazy in your $PATH, or you need the Exec line to run an interactive shell (to source bashrc) and then run the function (the former would make much more sense to me).

Last edited by Trilby (2018-05-29 15:24:00)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2018-05-29 15:22:13

Lone_Wolf
Administrator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 15,557

Re: [SOLVED] Weird behavior of functions defined in bashrc?

Create a custom menu entry that starts mendelazy .
If that works, cinnamon mendeley menu entry uses the default *.desktop instead of your changed one.


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.

clean chroot building not flexible enough ?
Try clean chroot manager by graysky

Offline

#8 2018-05-29 16:01:29

ahura
Member
Registered: 2016-02-20
Posts: 57

Re: [SOLVED] Weird behavior of functions defined in bashrc?

Trilby wrote:

You either need to put the content of that function in a script called mendelazy in your $PATH

I tried doing this - specifically, I created /home/ahura/.scripts/mendelazy with the contents

mendeleydesktop &
sleep 5
cpulimit -i -l 10 -p $(pidof /opt/mendeleydesktop/lib/mendeleydesktop/libexec/mendeleydesktop.x86_64)

ran chmod +x on it, and added export PATH="$HOME/.scripts:$PATH" in bashrc, and removed the function definition from bashrc. I rebooted just to make sure the old function was gone.

Once again, running mendelazy in terminal works as expected, whereas the menu item doesn't. Similarly, adding the full path (Exec=/home/ahura/.scripts/mendelazy) in /home/ahura/.local/share/applications/mendeleydesktop.desktop also doesn't change anything. It seems the menu item just does the same thing no matter what I put in the .desktop file.

Last edited by ahura (2018-05-29 16:06:39)

Offline

#9 2018-05-29 16:19:31

ahura
Member
Registered: 2016-02-20
Posts: 57

Re: [SOLVED] Weird behavior of functions defined in bashrc?

Okay, I tried simply deleting the menu shortcut and creating a new one executing the script, and this works. Thanks for the inputs.

To summarize solution for others with the same problem:

1) install cpulimit

2) create script somewhere with contents

mendeleydesktop &
sleep 5
cpulimit -i -l 10 -p $(pidof /opt/mendeleydesktop/lib/mendeleydesktop/libexec/mendeleydesktop.x86_64)

3) make script executable (chmod +x <filename>)

4) Delete existing menu item and create a new the runs the script.

Offline

#10 2018-05-29 19:21:16

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [SOLVED] Weird behavior of functions defined in bashrc?

Did you check to make sure the right desktop file is being used? What did htop say with regard to the processes you were running -- did your shellscript show up as the parent process for mendeleydesktop?


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

Board footer

Powered by FluxBB