Running task when system has no interactive use

Forum rules
Before you post please read this

Running task when system has no interactive use

Postby hpae on Mon Jan 06, 2014 7:56 am

I would like to run a rather CPU intensive task all the time when the system is not in interactive use but stop it when being used.
When run during interactive sessions, this might cause serious lag. Is it possible to do this?

The good enough approximation would be:
- start the task when screen is blanked
- stop it when it is unblanked
- start it on system start
- run it always when there are no users logged
- never run it (and kill it if running) when running on battery power.

For the last part I already found out I can follow the file /sys/class/power_supply/AC/online .

For screen blank, however, I am not able to find a reliable method.
Preferably this should work even without X windows running.
However, I am okay with a solution that only works in co-operation with X Windows. The task could be always run(with low priority) if there is no X windows running or nobody is logged in.

I found an almost solution by reading the status from the output of 'xset q'.
However, this requires:
- continuous polling of the output instead of watching status of a file via inotify
- X running ...

Does, for example, DPMS monitor off, when done by X windows, change/delete/create a file somewhere?
I am comfortable in writing upstart configuration files and whatever is needed.
I am familiar with Linux and have spent several years as sysadmin for Linux systems. However, this problem does not come up when administering servers.
hpae
Level 1
Level 1
 
Posts: 2
Joined: Mon Jan 06, 2014 7:19 am

Linux Mint is funded by ads and donations.
 

Re: Running task when system has no interactive use

Postby SiKing on Mon Jan 06, 2014 1:20 pm

From what you describe, this would require a lot of interaction with the application in question - which you did not mention. Does the app itself have the option to restart a process after you stop / kill it?
Have a look at 'cron' and 'nice', and see how far those get you. Have a look at some of the alternatives to cron to see of those would be of any help to you.
SiKing
Level 5
Level 5
 
Posts: 575
Joined: Mon Sep 29, 2008 10:57 pm
Location: Las Vegas

Re: Running task when system has no interactive use

Postby xenopeek on Mon Jan 06, 2014 4:57 pm

That sounds like quite a complex project. You can stop (pause) a process with `kill -STOP pid` and continue it at a later moment with `kill -CONT pid`. Replace pid with the pid of the heavy task.
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13679
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Running task when system has no interactive use

Postby bjornmu on Mon Jan 06, 2014 7:10 pm

What about simply running the job with 'nice'? I am running 6 parallel SETI@home jobs on my desktop with a 6-core AMD, those are very CPU-intensive jobs. They are *not* paused when the system is in use (they can be set to do so but I turned that off), so they are running at full throttle as I type this. The only thing they do to not interfere is to run with niceness on. And I can honestly say I don't notice any delay or slowness in what I do. The CPU core temperature is up about 2C but that's about it. :P

I suggest you try 'nice' and see how it works out. Plus the extra bit to stop the task when running on battery.
User avatar
bjornmu
Level 3
Level 3
 
Posts: 136
Joined: Wed Dec 19, 2012 2:50 am
Location: Trondheim, Norway

Re: Running task when system has no interactive use

Postby SiKing on Tue Jan 07, 2014 2:23 pm

xenopeek wrote:You can stop (pause) a process with `kill -STOP pid` and continue it at a later moment with `kill -CONT pid`.

I thought this assumes the application / process is listening for this signal?
SiKing
Level 5
Level 5
 
Posts: 575
Joined: Mon Sep 29, 2008 10:57 pm
Location: Las Vegas

Re: Running task when system has no interactive use

Postby bjornmu on Tue Jan 07, 2014 5:46 pm

SiKing wrote:
xenopeek wrote:You can stop (pause) a process with `kill -STOP pid` and continue it at a later moment with `kill -CONT pid`.

I thought this assumes the application / process is listening for this signal?


No, SIGSTOP cannot be ignored.
User avatar
bjornmu
Level 3
Level 3
 
Posts: 136
Joined: Wed Dec 19, 2012 2:50 am
Location: Trondheim, Norway

Re: Running task when system has no interactive use

Postby hpae on Thu Jan 09, 2014 4:02 am

Yes, In principle I could use SIGSTOP and SIGCONT although doing that is a bit more complex than simply doing service XXX stop and/or servce XXX start .
Since the task is supposed to be run even when nobody is logged in, it likely has to be an upstart job. It could be a cron job but that would not allow same kind of control.
Running it that way also has the added benefit of it automatically respawning if it crashes. And yes, as someone already mentioned SIGSTOP cannot be caught. The same also applies to SIGKILL. But that does not really answer the question I asked.

To the one who mentioned nice: yes, that is indeed an option for one of these tasks. (I actually have several) However, some of them also use hardware heavily in other ways and niceness is unlikely to help in there. Thus, the only option remains to actually shutdown(or STOP as someone suggested) the task when computer is in interactive use - the X window session is here the important thing in particular since it is more resource intensive than ssh login for example.

I already know about the xset q solution. However, that requires polling the X display continuously.
So, how to find out whether there is interactive use, by watching a file change/removal/creation?
This is possible for the battery status. I found the file by doing strace on on_ac_power .
hpae
Level 1
Level 1
 
Posts: 2
Joined: Mon Jan 06, 2014 7:19 am

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests