import subprocess r = subprocess.run(['crontab', '-l'], capture_output=True, text=True) lines = r.stdout.splitlines() new_lines = [] for line in lines: nl = line if 'cd /home/hmo/MoFin &&' in line and ('market_watch.py' in line or 'market_screener.py' in line): nl = line.replace('cd /home/hmo/MoFin &&', 'cd /home/hmo/MoFin/deploy/profile-scripts &&') print('CHANGED:') print(' old:', line) print(' new:', nl) new_lines.append(nl) p = subprocess.run(['crontab', '-'], input='\n'.join(new_lines) + '\n', capture_output=True, text=True) print('rc=', p.returncode)