From d08c353a653ed5c32371da1d1bff41e5be3826a5 Mon Sep 17 00:00:00 2001 From: David Robert Verelst <dave@dtu.dk> Date: Tue, 21 Nov 2017 21:26:36 +0100 Subject: [PATCH] do not overwrite existing crontabs, only remove current crontab --- launch.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/launch.py b/launch.py index d3f0064..273b7f0 100755 --- a/launch.py +++ b/launch.py @@ -97,7 +97,8 @@ def write_qsub_exe(fname, pbs, tsleep=0.25): return job_id def write_crontab(every_min=5): - """Create a crontab script, and submit to crontab. + """Create a crontab script, and submit to crontab. Do not overwrite + existing crontabs. """ python = '/usr/bin/python' launch = '/home/MET/repositories/toolbox/pbsutils/launch.py' @@ -108,9 +109,9 @@ def write_crontab(every_min=5): # when using the crontab option, cpu parameters are read from the config # file if it exists, but only the first run will build a pbslist file rpl = (every_min, cwd, python, launch) - crontab = '*/%i * * * * cd "%s"; %s %s --crontab --cache ;\n' % rpl + crontab = '*/%i * * * * cd "%s"; %s %s --crontab --cache ;' % rpl f = open(fpath, 'w') - f.write(crontab) + f.write(crontab + '\n') f.flush() f.close() @@ -125,17 +126,20 @@ def write_crontab(every_min=5): # current crontab job is already running! crontab_exists = True break - + # add: (crontab -l ; echo "cmd") | crontab - if not crontab_exists: - cmd = 'crontab %s' % fpath + cmd = "(crontab -l ; echo '%s') | crontab -" % crontab p = sproc.Popen(cmd, stdout=sproc.PIPE, stderr=sproc.STDOUT, shell=True) stdout = p.stdout.readlines() p.wait() print "added launch.py to crontab:" - print crontab + print crontab + '\n' def remove_crontab(): - cmd = 'crontab -r' + """Remove only the crontab refering to the current working directory + """ + cwd = os.getcwd() + cmd = "crontab -l | grep -v '%s' | crontab -" % cwd p = sproc.Popen(cmd, stdout=sproc.PIPE, stderr=sproc.STDOUT, shell=True) stdout = p.stdout.readlines() p.wait() -- GitLab