Using python to start programs after logging in

Cecil Westerhof Cecil at decebal.nl
Thu Jan 19 23:26:20 EST 2017


On Friday 20 Jan 2017 01:24 CET, MRAB 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))
>
> You don't have to pass a tuple to check_call; it'll also accept a
> list.

I did not know that. The same for Popen.


>> for command_arr in commands:
>> command         = command_arr[0].split()
>> log_directory   = command_arr[1]
>> directory       = command_arr[2]
>> if (directory != ''):
>> chdir(directory)
>
> There's no need for the parentheses around the 'if' condition.

Thanks, I always thought these were necessary.


>> 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()
>
> Using 'chdir' is generally a bad idea.
>
> 'Popen' will start the process and then continue, so what you're
> doing is changing the directory, starting a process, and then
> changing the directory again, possibly before the process has truly
> started (i.e. you've told the OS to start it, but there might be a
> small delay before that happens). Popen accepts a 'cwd' argument,
> which sets the 'current working directory' for that process.

I am using cwd now.


>> Sometimes a command needs to be executed in a different directory,
>> that is done like: commands = [ ['lein run', 'NONE',
>> 'Clojure/Quotes'], ['xfce4-terminal --maximize --execute screen -S
>> Clojure -c ~/.screenrcClojure', 'NONE', ''],
>> ]
>> do_desktop('6', commands, 30)
>>
> Why 'NONE'? Why not None?

I am using None now.


Thanks.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list