How to run a script 24/7

Muzan

Junior Member
Premium Member
Joined
Jul 14, 2017
Messages
115
Reaction score
70
Edit: The title is supposed to have a question mark behind it, so people know its a question and not a tutorial

I was thinking about running this "instabot.py" script 24/7 but both of my laptops always turn off randomly for no reason. And i dont want to have my stationary PC on all the time. Does there exist a way to run these scripts on online servers? Someone told me to run scripts on a "Virtual Server" but i dont even know what that means, and is it possible to run scripts on a "Virtual Server" for free?
 
You could compile the script as exe and run it using windows scheduler to keep it always on. You can find all the details with some quick google sesrches.
 
Run VPS isn't free. VPS cost around 20-50$/month. But you can configure one of your laptops. I think they turn off because you don't changed battery configs, by default this configurations has restrictions for working time.
 
Your script may have problems that are causing memory leaks causing an eventual crash?
 
I don't know what kind of script this is and how much power you need to run it, but if you want something for free you can always get an Amazon VPS for free for 1 year.
 
Yes plenty of great vps sellers here at bhw so shouldn't be hard to find a decent and cheap solution :)
 
If its a python script, you can get a $5 VPS and run it 24*7 with no issues.
 
Personally I would get a $5 droplet from Digital Ocean and choose CentOS for an OS because it has the longest support cycle of any distro. Python is probably pre-installed. If it isn't it you should read this page: https://danieleriksson.net/2017/02/08/how-to-install-latest-python-on-centos/

Then you need to verify if it your script is running periodically. Any script can exit for a number of reasons. If you need to be sure it will always be running you create a cron job that checks on it.

The cron tab is one line in the file /var/spool/cron/root and looks like this:
* * * * * cd /path/to/your/monitor/script; ./your.monitor.script.pl > /dev/null 2>&1

To edit this file:
# sudo vim /var/spool/cron/root

If vim is not installed you can try nano instead of vim. Or install vim:
# sudo yum install vim

The .pl extension in your.monitor.script.pl is because I would write it in perl, my preferred choice for an application like this.

The perl script looks like this:
#!/usr/bin/perl
# this script is called once per minute by a cron tab to ensure that the python script is running
my $response = readpipe( 'ps awx |grep \.py$' ); # this will list all the python processes into a variable called $response
exit if $response =~ /your.python.script.py/; # this will scan $response for the name of the script and exit if the script is running, preventing the next line from executing
readpipe("/path/to/python/script.py > /dev/null 2>&1 &"); # this will start the python script, disconnect if from this process and direct the output to a black hole (/dev/null) before exiting

Than's how I would do it, but it may not be the ideal solution for everyone. Running a 24x7 script from a laptop is not practical. You are going to need your laptop for some purpose at some point and have to stop the script.

Also once you have your own server you will get all kinds of nifty ideas and be able to implement them.

My two cent's worth
 
You might also want to look into pm2. It will auto-start the script on crash and on boot. It's also an easy to use, mature software so you should find help for it easily. (example)
Like others have suggested, better to run the script on a VPS instead of your own computer. Most will offer you 99%+ uptime and you can rent one for $5/month. Check out digitalocean, they've been around for a while and the support is good.
 
Back
Top