What is causing Xorg high CPU usage? - cpu

I am running feh image viewer on Debian and after some hours of normal CPU usage (3% aprox.) , xorg suddenly starts using much more CPU (80% aprox.) and everything runs very slowly. I am not running anything else so the bug should be either on feh or on the xserver...
I am using the command "feh -z -q -D20 -R 1" (-z for random image, -q for quiet, -D20 to change the picture every 20 seconds and -R 1 to refresh the directory every second, as I erase and insert pictures pretty often)
When I use the command "free -m" before the high CPU usage and feh running, I get
total used free shared buff/cache available
Mem: 923 117 474 19 331 735
Swap: 99 0 99
And after several hours I get the same for "mem" but the used amount of "swap" is 99.

The fact that your memory usage goes up (swap is full) points directly to memory leak in some program in your system. Considering that feh is not probably designed for such an use case I'd bet it's the cause for going out of memory.
The "everything runs slowly" is caused by kernel going out of memory and it's doing its best to keep the system running. If you insist on runnin feh your choices are
Triage the memory leak bug in feh and create a fix for it.
Try to get somebody else do the same for you.
Periodically kill feh and rerun it again. Basically you can do (in bash)
while true; do timeout 120m feh -z -q -D20 -R 1; sleep 2s; done
which will kill every 120 min and restart it after 2 second delay (which allows you to kill the while loop if needed). Another choice would be to use ulimit to set maximum amount of memory you want to allow for feh and the process probably simply dies once it's using too much.

I solved this problem, but I don't know why, too.
You can try run this code kill this process:
ps -a | grep Xorg | awk '{print $1}' | xargs kill 9

Related

How to compare if the cpu serial is correct

I have a question. I would like to check when the linux system starts or if the serial cpu is correct.
If not, he would be rebooting. So he would be doing reboot loops all the time.
I found the command to check the serial cpu command:
cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
How to compare the result of this command to the value of eg 000000ddd0d0d??
And I do not know how to look like such a check script and where to put it in the Ubuntu system (/etc/init.d/rc.local ??).
It is correctly??:
#!/bin/bash
STR=cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
if $STR != '000000ddd0d0d'; then
reboot
Thank you for your help
Sorry for my English.
You probably should do the whole thing in one grep command:
grep -q '^Serial.*000000ddd0d0d' /proc/cpuinfo || reboot
This will reboot unless a line with Serial and 000000ddd0d0d in it is found in /etc/cpuinfo.
BUT this is questionable for several reasons.
What should a reboot be good for in such a case? As this is supposed to be done during machine startup, you will then enter an infinite loop of reboots which can only be stopped by switching the computer off. This is horrible! You probably never encountered such a problem (as admin) or you wouldn't produce it voluntarily. The only way to fix such a system is to boot it via some other medium (USB thumb drive or similar).
Not all Linux distributions or kernel versions offer a CPU serial number in /proc/cpuinfo. My current system, for instance, doesn't. So on my computer this would not work at all.
The whole idea of reacting on a CPU serial number is highly questionable as a CPU might break (not likely but possible) and then probably is replaced and the new CPU, while surely having a different serial number, should not pose any trouble.
So, I think you might want to reconsider.

How to free up memory after running a process in a shell

I have a script that runs a JAVA process that loads data into a database every 10 secs using a loop. This script seems to work perfectly, but after a couple of days I start getting Memory issues. If I stop the script everything frees up, I can start it again and it will run happily for another couple of days.
RUNME=Y
PROPERTIES=someproprties.properties
CHECKFILE=somelockfile.lock
touch $CHECKFILE
while [ "$RUNME" = "Y" ]; do
if [ -f $CHECKFILE ]
then
#Run Process
$DR_HOME/bin/dr -cp $CP_PLUGIN -Xmx64g --engine parallelism=1 --runjson $HOME_DIR/workflows/some_dataflow.dr --overridefile $PROPERTIES 1> /dev/null 2>> $LOG_FILE
#Give Process a little time to finish up before moving on
sleep 10s
else
RUNME=N
fi
done
I had assumed that once the process had run it would make any memory that it had allocated for the process available again, so that the next iteration of the loop could use this. Given that this does not seem to be the case, is there a way I can force the release of memory post the running the process. I appreciate that this may be something that I need to address in the actual JAVA Process rather than in a Shell - but as this is the area I have more control over, I thought I would at least ask.
To check the processes which are running and memory used
sid=$(ps -p $$ -osid=)
while ....
ps --sid $sid -opid,tty,cpu,vsz,etime,command
vsz shows the virtual size used by the process
Then if it's really bash process, it may be environment which is growing, but from the script it can't be that.

Is there a way to see how much time remains on a timed process?

Is there a way to determine (in bash) how much time is remaining on a process that is running for a specified time?
For example, some time after executing
caffeinate -s -t 8000 &
is there a command or technique for determining when my system will be allowed to sleep?
Bash won't know that caffeinate has a timer attached to it; for all it knows, -t refers to the number of times you'll place an Amazon order of Red Bull before the process exits five minutes later.
If you know this, however, you can detect when the command was started and do the math yourself.
$ sleep 45 &
[1] 16065
$ ps -o cmd,etime
CMD ELAPSED
sleep 45 00:03
ps -o cmd,etime 00:00
/bin/bash 6-21:11:11
On OS X, this will be ps -o command,etime; see the FreeBSD ps man page or Linux ps docs for details and other switches/options.

How to limit allowed time for a script run as a sub process in ksh script

I try to limit the allowed of a sub process in a ksh script. I try using ulimit (hard or soft values) but the sub process always break the limit (if take longer than allowed time).
# value for a test
Sc_Timeout=2
Sc_FileOutRun=MyScript.log.Running
Sc_Cmd=./AScriptToRunInSubShell.sh
(
ulimit -Ht ${Sc_Timeout}
ulimit -St ${Sc_Timeout}
time (
${Sc_Cmd} >> ${Sc_FileOutRun} 2>&1
) >> ${Sc_FileOutRun} 2>&1
# some other command not relevant for this
)
result:
1> ./MyScript.log.Running
ulimit -Ht 2
ulimit -St 2
1>> ./MyScript.log.Running 2>& 1
real 0m11.45s
user 0m3.33s
sys 0m4.12s
I expect a timeout error with a sys or user time of something like 0m2.00s
When i make a test directly from command line, the ulimit Hard seems to effectively limit the time bu not in script
System of test/dev is a AIX 6.1 but should also work other version and on sun and linux
Each process has its own time limits, but time shows the cumulative time for the script. Each time you create a child process, that child will have its own limits. So, for example, if you call cut and grep in the script then those processes use their own CPU time, the quota is not decremented from the script's, although the limits themselves are inherited.
If you want a time limit, you might wish to investigate trap ALRM.

sh via Ruby: Running ulimit and a program in the same line

I am trying to run some computational-intense program from Ruby via the following command:
%x(heavy_program)
However, I sometimes want to limit the running time of the program. So I tried doing
%x(ulimit -St #{max_time} & heavy_program)
But it seems to fail; the "&" trick does not work even when I try it in a running sh shell outside Ruby.
I'm sure there's a better way of doing this...
use either && or ;:
%x(ulimit -St #{max_time} && heavy_program)
%x(ulimit -St #{max_time}; heavy_program)
However using ulimit may be not what you really need, consider this code:
require 'timeout'
Timeout(max_time){ %x'heavy_program' }
ulimit limits CPU time, and timeout limits total running time, as we, humans, usually count it.
so, for example, if you run sleep 999999 shell command with ulimit -St 5 - it will run not for 5 seconds, but for all 999999 because sleep uses negligible amount of CPU time

Resources