Starting a Background Process for a Python code - python

I am currently running a python code from the command line with this command:
python fileName.py myUserID myPassword > logFile_date
Is there a similar command that I can use to either run this process in the background or spawn a process to run in the background?
Thanks,
Ramani

You can use a cronjob to run a script in the background. You can specify it to run at certain intervals or at certain times (here's the ubuntu documentation)
For example, here's what your cronjob might look like:
*/10 * * * * /your/path/to/python /path/to/fileName.py >> /path/to/logFile_date.txt
This will run your script every 10 minutes and have the output saved to logFile_date.txt, all in the background.

Related

How to run python script in linux terminal console and continue using command line?

My question seems to be quite easy, but for some reason I did not find a quick answer to it. I have a python script that I want to run on the terminal command line (Ubuntu linux server), which works for me. But then I can't use the command line until the script ends. The script takes a long time to run, and I would like to continue using the command line to perform other tasks. How can you do the work of a script when its progress is not shown on the command line, but keep its work? And how can I see the active processes that are running on the server to see if a process is running?
Run script command:
python script.py
Add next with & echo "123":
The script takes a long time to run, and I would like to continue
using the command line to perform other tasks.
It seems that you want to run said process in background, please try pasting following
python script.py &
echo "123"
it should start your script.py and then output 123 (without waiting until script.py ends)
how can I see the active processes that are running on the server to
see if a process is running?
Using ps command
ps -ef
will list all processes which you would probably want to filter to get interested one to you.

Cron scheduling a Python script, selecting correct output

I am trying to schedule running a Python module in cron. This question is somewhat similar to this but I think asks for a different use case.
Task
I have a Python script I run in the shell as a module like this:
python -m myscript
It prints a bunch of numbers (via print) and works when I run it from the shell.
Question
I am now trying to run this every minute using a cron job, like so:
*/1 * * * * python -m myscript
Question1: This does not print to the terminal as expected / I don't see any output. Why? (To test if it is running at all, I redirected output to a file, which creates an empty file every minute).
Question2: My thinking was that any command that works when I run it manually in the shell will also work the same way when started via cron. Is that mistaken? E.g do I still have to make the script executable and such?
Question3: Thinking about it, I was not quite sure where cron will direct e.g. a print / stdout command and could not find in the docs. Do I have to / Should I manually specify the output target if I want it to print to a new shell window?
Currently running this on elementary OS but I want to eventually migrate to a Raspberry Pi. Any help is much appreciated!

Trouble executing a python 3 script every minute with cron

I am attempting to run a python 3 script every 1 minute using cron on a raspberrypi 3, for testing, where eventually it will just be run once a day.
To start, I made a new cron job using: sudo crontab -e, and typed in the following code for a once a minute job:
*/1 * * * * /home/pi/folder/file.py
Then I saved and closed and waited. My python script emails me text when executed, so I should have seen an email come in. It runs fine (and emails me) when I execute it manually outside of cron.
So, what am I doing wrong with cron for it not to run? And do I need to make the python file executable or something with chmod?
Possible duplicate of Execute python Script on Crontab
EDIT:
Adding comment here since the comment box mangled my formatting.
In your example above it looks like you are just trying to "run" the file. You need to call the python executable, and pass it an argument that points to your file.
From the StackOverflow comment mentioned above look at this crontab entry:
*/2 * * * * /usr/bin/python /home/souza/Documets/Listener/listener.py
Take a look at the first part of the command /usr/bin/python this is pointing to the python executable not just to the .py file you want to run.

Automatically restart a .py every 30 minutes

Recently I set up a twitter retweet bot, but after few minutes it's going down.
I used this python bot and I'm using "nohup python mrbot.py &" to keep it running in the background after closing the terminal. Even if I use it as "python mrbot.py", it stops working after around 30 minutes. So could someone please tell me how to automatically restart it every 30 minutes?
I'm a very beginner at this. *Remember I'm running it in background using "nohup". I don't know if this helps.
Crontab would be the ideal way to do it. Please search for crontab and how to enable and schedule a crontab job.
For your example a crontab entry would look like
30 * * * * /path/to/your/python/script.py
Since you are running ubuntu, you should have python in your path and have a shebang in the python script. A shebang is the first line in your python script with path to the python executable, it is in following format
#!/usr/bin/python
You are on Ubuntu, so you can use crontab command. Open terminal and type:
crontab -e
This will prompt you with (probably) an empty document. Then write the following line:
*/30 * * * * /usr/bin/python script.py
This will make sure your script runs every 30 minutes.
You can read more about crontab and what it does and how it works here

Cron and Facebook Friend Tracker not willing to work together

I've been trying to automatically execute the script facebook-online-friend-tracker, which opens chrome, logs into facebook and writes the number of online friends onto a .csv file (https://github.com/bhamodi/facebook-online-friend-tracker).
I wrapped it into a script that I've called facebooktracker.
When I execute ./facebooktracker manually from the terminal, everything works fine. But since I want to collect some statistics, I have set up a cron job to work every 10 minutes.
By using: crontab -e, I've set:
*/10 * * * * /home/enrico/facebooktracker
and it does not work, meaning that it doesn't write on the .csv file (syslog shows that the command has been executed though).
I have tried to use Cron to execute a simple script that writes "Hello world" on a file and it works fine, I've tried to use Cron to open a GUI application and it works fine.
Therefore it looks like the script works fine, cron works fine, but they are not willing to work together.
Things that I have tried (yet to no avail) are:
*/10 * * * * env DISPLAY =:0 /home/enrico/facebooktracker
*/10 * * * * env DISPLAY =:0 /home/enrico/facebooktracker > /dev/null 2>&1
Directly use the script facebook-online-friend-tracker without wrapping, by setting in cron:
*/10 * * * * /home/enrico/anaconda2/bin/facebook-online-friend-tracker --user "username" --password "password" --path "path"
Add echo "Hello world" at the end of the facebooktracker script, setting the output to a .log file ( >> facebooktrackerlog.log) and it does write "Hello world", but still doesn't write the number of online facebook friends on the .csv file
I've run out of ideas. Anyone has a clue? I'd really appreciate it. Thanks!
When you run it manually it's running as you with any .profile etc loaded. When cron runs it your profile hasn't been loaded. Try loading your profile as the first part of the cron job.
I'm the author of the facebook-online-friend-tracker script. I see that you had some trouble setting up a cron job to execute the script every 10 minutes. You'll be happy to know that as of v2.0.0, I've implemented scheduling as part of the script. You no longer have to set up cron jobs or task schedulers. Simply run the script once and follow the prompts. To upgrade, simple run:
pip install facebook-online-friend-tracker --upgrade

Categories

Resources