Using python to start programs after logging in
Cecil Westerhof
Cecil at decebal.nl
Thu Jan 19 23:34:45 EST 2017
On Friday 20 Jan 2017 00:36 CET, Cecil Westerhof wrote:
> I wrote a function for switching to the correct virtual desktop and
> starting all the commands. I am also using with now:
> def do_desktop(desktop, commands, seconds_to_wait = 10):
> desktop_command = ('wmctrl -s ' + desktop).split()
> check_call(tuple(desktop_command))
> for command_arr in commands:
> command = command_arr[0].split()
> log_directory = command_arr[1]
> directory = command_arr[2]
> if (directory != ''):
> chdir(directory)
> if (log_directory == 'NONE'):
> Popen(tuple(command.split()))
> else:
> log_file_name = log_directory.replace('%T', strftime('%F_%R'))
> with open(log_file_name, 'w') as log_file:
> Popen(tuple(command), stdout = log_file, stderr = STDOUT)
> if (directory != ''):
> set_default_dir()
> sleep(seconds_to_wait)
I wrote a little better version:
def do_desktop(desktop, commands, seconds_to_wait):
desktop_command = (switch_desktop + desktop).split()
if seconds_to_wait == 0:
seconds_to_wait = 10
check_call(desktop_command)
for command_arr in commands:
command = command_arr[0].split()
log_directory = command_arr[1]
directory = command_arr[2]
if not log_directory:
log_directory = '/dev/null'
log_file_name = log_directory.replace('%T', strftime('%F_%R'))
with open(log_file_name, 'w') as log_file:
Popen(command, stdout = log_file, cwd = directory, stderr = STDOUT)
sleep(seconds_to_wait)
When there is no log_directory I set it to '/dev/null': that makes the
code a little bit cleaner.
And everything is now fetched from the database:
for desktop in cursor.execute(select_desktops).fetchall():
desktop_name = desktop[0]
desktop_value = desktop[1]
desktop_wait = desktop[2]
commands = cursor.execute(select_commands, [desktop_name]).fetchall()
do_desktop(desktop_value, commands, desktop_wait)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
More information about the Python-list
mailing list