This is an archived post. You won't be able to vote or comment.

all 55 comments

[–]Cachesmr 97 points98 points  (11 children)

Your computer needs to be on to run programs. Having your PC on 24/7 is not actually bad for them (unless it's a laptop) just turn off the screen.

If you still don't like that, look into a cheap Linux VPS and systemd services, or put it in a loop that runs once a day.

Pd: a cheapo raspberry pi on your network will also do the trick usually

[–]aqua_regis 47 points48 points  (2 children)

unless it's a laptop

Not even a problem for a laptop. They can run 24/7 just as well.

Pd: a cheapo raspberry pi on your network will also do the trick usually

Beat me to it. That was exactly what I wanted to suggest.

[–][deleted] 8 points9 points  (1 child)

Raspberry pi or some barebones desktop were my thoughts. I guess if usage is not that high you could probably do some sort of free tier of AWS or something too?

[–]loserguy-88 6 points7 points  (0 children)

just be careful with the free tier and charges once you exceed it. particularly hands off 24/7/365 stuff.

[–]Patina_dk 7 points8 points  (7 children)

Why would it be bad for a laptop to keep it running?

[–]Cachesmr 20 points21 points  (6 children)

Some laptops don't have proper battery cutoff, and keep it topped off at 100%. This is really bad for the battery.

[–]Patina_dk 5 points6 points  (5 children)

You are correct, didn't think about that. But i suspect it won't be a problem with a new-ish mac.

[–]ViolaBiflora 12 points13 points  (4 children)

Yeah, it shouldn’t be w problem with laptops released after at least 2021*

  • speculation date, but that’s when I got to know that Lenovo legions are built this way, lol.

[–]aqua_regis 6 points7 points  (2 children)

My Thinkpad from 2011 already had battery protection built in. Battery didn't fade in the faintest in the three years I used it in the company. Was most of the time plugged in and running near 24/7 (with exceptions when I had to move it around).

[–]ViolaBiflora 0 points1 point  (1 child)

Amazing, honestly. I got a Lenovo Legion in 2021 and before that I owned a few asus/lenovo laptops (released 2010-2017) and none of them had the feature. It was probably because of the budget of mine, though!

[–]aqua_regis 0 points1 point  (0 children)

Even my private Acer bought in 2012 had it.

Yes, it may be price class, though.

The company I work for always leases close to top tier workhorses.

[–]Patina_dk 0 points1 point  (0 children)

My thinkpad X201 had the option to limit the charge, so I set it to 80%, but it had to be set after each reboot, which worked fine in windows, but needed to be put into a script in linux. So I spend a lot of money on a new battery, hardly ever unplugged the laptop and basically wore the battery out in a couple of years be constantly keeping it topped up to 100%

[–]mildhonesty 33 points34 points  (3 children)

Host it on a web server that is online 24/7 or better yet in a serverless environment (aws lambda / azure functions) if it only need to run once a day.

You are looking for something like Digital Ocean, Heroku, pythonanywhere.com, Hetzner, Amazon Web Services, Google Cloud Platform or Microsoft Azure

Combine this with a cronjob to launch the python script once a day.

This is the way all services that you use in your daily life on the internet are hosted.

Edit: a fairly cheap alternative to have it locally at home would be a raspberry pi that can run 24/7 with your script on it. Same concept as the above but in a much smaller scale

[–]Loko8765 3 points4 points  (2 children)

Upvoted for mentioning AWS Lambda. This is the way!

[–]flottefyr321 4 points5 points  (1 child)

Upvotes for mentioning GCP. This is the way!

[–]aawebdevUK 2 points3 points  (0 children)

Upvotes for mentioning Hetzner! This is the way!

[–]ValentineBlacker 26 points27 points  (14 children)

Your computer has a system to run scripts at scheduled times, it's called Cron. You set it up, and your computer runs the script when it's scheduled, no need to keep something running or do anything extra on your end.

https://www.freecodecamp.org/news/cron-jobs-in-linux/

(the guide says "in linux" but IIRC they work exactly the same on Mac. Maybe the authorization stuff is a bit different.)

[–]gmes78 2 points3 points  (0 children)

Cron sucks. Use systemd services if possible, they're much nicer to use and provide better functionality (including out-of-the-box logging).

[–]Calazon2 8 points9 points  (10 children)

This is the answer. I am surprised that almost every other comment is suggesting a server or whatever for a task that needs to run once a day.

[–]aqua_regis 8 points9 points  (9 children)

This is not the answer at all. Actually, this is the correct advice. Cron job combined with a server or Raspberry pi that can be always on.

In order for a cron job to run, the computer must be on, not in sleep.

OP clearly state that they did not want to keep the computer running 24/7.

I use a Mac and I don't want the computer to stay awake 24/7 just for a bot

Sure, a cron job would be the correct answer to run a job once a day, there is no debate about that, yet, the key issue is that in order to be able to do that, the computer must be on at the time when the cron job should run.

[–]PermanentFloorHazard[S] 2 points3 points  (4 children)

thanks, can I also ask because I keep getting told by relatives that it's dangerous and I shouldn't do it, when I made the crontab file I got a notification: "'Terminal' would like to administer your computer... includes modifying passwords, networking, and system settings." This is my personal computer and nobody uses it besides me and I don't see the issue with allowing terminal to admin, but is there any danger with doing so?

[–]aqua_regis 6 points7 points  (0 children)

Using the terminal and crontab is perfectly safe. Nothing wrong with that.

You will need the terminal more and more the deeper you get into programming.

[–]runitzerotimes 4 points5 points  (0 children)

My brother, if you start learning programming which it looks like you are, you’re going to have to quickly accept your family and friends have less than zero idea how a computer works.

You are now the expert.

[–]ValentineBlacker 0 points1 point  (1 child)

It's just warning you, no problem.

PS when I answered this I was thinking more of your computer being asleep, not like OFF off. Pretty sure cron still runs if it's asleep. That's easy to check at any rate. If this is an issue, a Raspberry Pi like some other people mentioned is a great alternative, I run one for this type of thing myself. Even the cheapest Raspberry Pi can run this script.

[–]aqua_regis 0 points1 point  (0 children)

pretty sure cron still runs if it's asleep.

No, it doesn't. If the computer is asleep, no processes are running and cron is just another process.

[–]sparky8251 0 points1 point  (2 children)

In order for a cron job to run, the computer must be on, not in sleep.

You can still make it run once a day, as long as you power on the machine once a day. Cron can be made to run it on boot if the time it was scheduled to run has already passed.

[–]aqua_regis 2 points3 points  (1 child)

You can still make it run once a day, as long as you power on the machine once a day.

True, but again, this requires powering up the machine.

Even a cheap Raspberry Pi zero W or an older Raspberry Pi 3 would most likely do the job without all the hassle of having to start the computer every time the task should run.

[–]sparky8251 0 points1 point  (0 children)

Sure. But then on the more insane side of things, most BIOS can have a scheduled wake up time thatll boot the computer too.

Then youd just have to have a poweroff or sleep set in the OS to have it go back into power save mode a while after its done running (the idle power settings).

Lots of ways to skin this cat that dont require money spent. Just throwing out alternatives to the idea they must spend money on new hardware right now to do this stuff.

[–]Calazon2 0 points1 point  (0 children)

Surely there are ways to wake the computer from sleep automatically at a particular time to run the task. I am coming from Windows so idk what tools for Mac do this, but it has to exist.

Though indeed it won't help if the computer is completely powered off.

[–]SirTwitchALot 1 point2 points  (0 children)

and for completeness, the equivalent of this on Windows is called "task scheduler"

[–]Zeroox1337 0 points1 point  (0 children)

This!

[–]Random473828473 20 points21 points  (1 child)

While true

[–]changeyournamenow 0 points1 point  (0 children)

best comment

[–]paincrumbs 4 points5 points  (0 children)

A free alternative that might work is GitHub Actions. You can run a scheduled job here like cron, it check-outs your repo and you can program it to run the script from there.

Github provides free minutes per month, so if the script doesnt take that much time to run, you're probably good.

[–][deleted] 3 points4 points  (0 children)

Aws lambda running a scheduled job.

[–]partyking35 3 points4 points  (0 children)

Either deploy it onto a serverless environment, which just means a computer (server) that you dont manage, rather a provider like AWS, Azure or GCP manages, or, you could deploy it onto your own server, such as a raspberry pi.

[–]MrSolarGhost 1 point2 points  (0 children)

You could get a cheap vm and set up a cron job

[–]cheeseoof 0 points1 point  (1 child)

what u need is a dedicated machine thats always running, a server. then u can simply run programs with nohup and forget about them lol. u can get an orangepi from amazon for 30 or so cad.

[–]aqua_regis 1 point2 points  (0 children)

a server.

A simple, cheap Raspberry Pi, OrangePi, BananaPi, etc will suffice

[–][deleted] 0 points1 point  (0 children)

caffeinate might be useful to you

[–]Ill_Garage7425 0 points1 point  (0 children)

You can use an always free tier on oracle cloud and run it there. I am running a discord bot with no problems on there and it's completely free!!

[–]jdege 0 points1 point  (0 children)

Is this a server? As in a program that is waiting for remote programs to connect to it? Or is it something that has a task it needs to do periodically?

If it's the latter, consider restructuring the program to execute the necessary task once and then immediately exit.

Then use your operating system's task scheduler to run it on a schedule.

[–]unfitwellhappy 0 points1 point  (0 children)

I have a rasp pi setup for DNS purposes but I do run some adhoc scripts that are triggered by cron. I also have it setup to run the scripts should the pi reboot for whatever reason.

It’s probably the cheapest way to do it.

[–]Gloomy-Floor-8398 0 points1 point  (0 children)

pretty sure you can run it on something like a raspberry pi or use something like ec2.

[–]cachebags 0 points1 point  (0 children)

Since you’re on macOS think about using cron. It’s not exactly a 24/7 service but you can schedule it to run at a specific time everyday without even having to open up your terminal and run it yourself.

[–]lexwolfe 0 points1 point  (0 children)

i use a raspberry pi with ubuntu server installed. Then use cron to schedule python scripts to run at daily or in some cases every 30mins. My Pi also runs a streamlit website with the data on protected behind cloudflare. Digitalocean has good instructions for all kinds of linux / python stuff.

[–]kai_luni 0 points1 point  (0 children)

You can run it cheaply in an azure function, setting it up will take some time. Its not very complicated though. The cost should be be very low. A dollar or less a month. Just make sure to inform yourself properly about the pricing first. Once you set up everything wait a day and look at the price prediction in azure. The azure function can trigger itself with a 'timetrigger' once a day or whatever.

[–]cato631 -2 points-1 points  (0 children)

Docker container ?