Programmatically disown/nohup in Python
<p>I want to run a little script that pings a server once every X seconds/minutes and if it doesn't get the expected response, send an email to the specified email address to notify me the server is down.
Obviously, I want to run this script on some server with a nohup option, so that it stays alive when I disconnect. I'm using smtplib (<a href="https://docs.python.org/3.5/library/smtplib.html" rel="nofollow">https://docs.python.org/3.5/library/smtplib.html</a>) to send the email, all works great. But because I do not want to hardcode the password for my email account in the script, I want to provide it to the python script interactively. That is where I didn't get it to work in combination with the nohup option. (e.g. running <code>nohup pingServer.py -u &lt;username&gt; -p &lt;password&gt; &amp;</code> seemed to kind of cancel out the idea of nohup/background. After specifying the password and then disconnecting the terminal, the process still seemed to have stopped.</p>

<p>So I wrote a little bash script around this to handle the passing of the username and password, and still being able to do it in a nohup/disown way. Here's my bash code:</p>

<pre><code>#!/bin/bash

read -p "Please enter sender email: " sender
stty -echo
read -p "Plese enter sender password: " passw; echo
stty echo

echo "INFO: Starting pingServer.py now."
echo
python pingServer.py  -u $sender -p $passw &amp;
disown
</code></pre>

<p>Which works great and does exactly what I want.
Untill, when I did some sanity checks, I noticed that the output of <code>ps -ax | grep py</code> gave me:</p>

<p><code>27489 pts/4    S      0:00 python pingServer.py -u &lt;username&gt; -p &lt;password&gt;</code>
where both my email and my password show up in plaintext in the terminal window. Seeing that the process runs on a server, this is definitely not something I want.</p>

<p>Does anyone have some ideas on how to get around this?
I enjoyed the exercise of writing this little script and liked the small challenge, hence did it in this way. But probably there are much better ways of achieving this small kind of notification service for when a server/service is down. </p>

<p>If anyone could give me some pointers on to how to get around this displaying of my password in plaintext and passing it to the python script in a secure way in combination with no hangup, that would be great (i.e. programmatically disowning/setting a nohup in python after the interactive prompt stuff has been done, although it may of course very well be that this is just plain impossible). Or perhaps it is possible to pass the password to python in some obfuscated/encrypted way?</p>

<p>Alternatively, any tips on how to achieve the same (e.g. getting an email when a server doesn't return the expected response) would also be welcome (although doing it yourself/getting it working yourself is much cooler of course :))</p>

<p>You can give your password as encrypted using openssl.</p>

<p>For example :</p>

<pre><code>echo $(openssl passwd -crypt mypassword)
</code></pre>

<p>Output :</p>

<pre><code>eXzWQlhzBJZ9.
</code></pre>

<p>Similarly You can give it as -</p>

<pre><code>nohup pingServer.py -u &lt;username&gt; -p $(openssl passwd -crypt mypassword) &amp;
</code></pre>

<p>This way you can encrypt your password. I hope it helps.</p>

python