Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/scripts/kill_old_procs.py
Views: 687
#!/usr/bin/env python1###############################################################################2#3# CoCalc: Collaborative Calculation4#5# Copyright (C) 2016, Sagemath Inc.6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <http://www.gnu.org/licenses/>.19#20###############################################################################2122# AUTHORS: William Stein and Harald Schilly2324# This has never been tested, and I think that for our purposes (on compute machines) it may be a bad idea in practice.2526# NOTE: needs psutil, which is a nonstandard package that is absolutely required for this script to work: pip install psutil2728import psutil, time2930onedayago = time.time() - 24 * 60 * 60313233def kill_old():34for p in psutil.process_iter():35if p.create_time < onedayago: continue36if len(p.username) != 8 or p.username == "whoopsie":37continue38print "Killing", p39p.terminate()404142def monitor(interval):43"""Run kill_old, then wait interval seconds."""44try:45kill_old()46except Exception, mesg:47# We really don't want this script to die, ever!48print "Ignoring exception raised during attempt to kill old -- ", mesg4950time.sleep(interval)515253if __name__ == '__main__':54monitor(1800)555657