CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/scripts/kill_old_procs.py
Views: 687
1
#!/usr/bin/env python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
# AUTHORS: William Stein and Harald Schilly
24
25
# This has never been tested, and I think that for our purposes (on compute machines) it may be a bad idea in practice.
26
27
# NOTE: needs psutil, which is a nonstandard package that is absolutely required for this script to work: pip install psutil
28
29
import psutil, time
30
31
onedayago = time.time() - 24 * 60 * 60
32
33
34
def kill_old():
35
for p in psutil.process_iter():
36
if p.create_time < onedayago: continue
37
if len(p.username) != 8 or p.username == "whoopsie":
38
continue
39
print "Killing", p
40
p.terminate()
41
42
43
def monitor(interval):
44
"""Run kill_old, then wait interval seconds."""
45
try:
46
kill_old()
47
except Exception, mesg:
48
# We really don't want this script to die, ever!
49
print "Ignoring exception raised during attempt to kill old -- ", mesg
50
51
time.sleep(interval)
52
53
54
if __name__ == '__main__':
55
monitor(1800)
56
57