Run console commands after x minutes

Is it possible that I could get a scheduled command (refresh) to run every x seconds for my npm project?

Cron is a tool that allows you to execute something on a schedule. There are many cron-related npm packages available, which you’d have to use alongside a method to keep your project alive. Or you could use a third-party cron service like https://cron-job.org/ or UptimeRobot to trigger a command via a HTTP request and run the command using child_process.exec.

I use UptimeRobot to keep it running but not refreshing (at least i think). I’m not entirely sure about what you said on how to set it up. Is there a tutorial or a more in depth way you could show me how to run the refresh command every X minutes?

thismay be a shitty solution, but this js actually forces the glitch’s main script to restart by throwing an error:

const resetter = setTimeout(function() {
console.log(Restarting Glitch project...);
throw void 0;
}, 5 * (60000));

Add at the top of your JS file:

var child_process = require('child_process');

Then within the route you have defined that UptimeRobot pings add:

child_process.exec('refresh', function(error, stdout, stderr){
	console.log(stdout);
});
1 Like

It works! Thank you very much!

Hi @Gareth today I’m starting to try glitch and it seems great.

I need an “hello world” guide to do a cron job.

My goal is very simple:

  • to run a wget command everyday, to update a CSV file I need in the root of my project;
  • to run a restart to update my project.

Could I write a bash script to run wget and call it in some way everyday using cron-job.org?
How to restart my project after wget update?

Thank you very much

Hi @aborruso!

Glitch apps wake on (web) traffic and sleep after about 5 minutes of no (web) traffic. So if you want to do something every 24 hours, using another tool to generate the web traffic to wake the Glitch app would be reasonable.

I created ~round-digit, which might be helpful as a “hello world” guide.
It fetches a weather report for Tehran when web traffic hits the /refresh route. It sends the most recent weather report fetched when web traffic hits the / route. As you can see, it doesn’t have any scheduled-every-24-hours functionality, you would use another tool for that.

Hope this helps,

Johnicholas

1 Like

Thank you very much @Johnicholas!

I’m using a Python based glitch https://glitch.com/edit/#!/holly-sombrero?path=glitch.json:1:0

And I have no idea on how to set up route on it; I think I cannot use your nodejs way. Am I wrong?

I’m sorry, Python is not as well-supported on Glitch as Node / Javascript, and I’m not as familiar with how to get things done in Python.

Datasette is to some extent an bundle of technologies (such as Sqlite for storage and queries, libuv for events, Vega for rendering charts in web browsers, and so on), which makes doing the standard thing one does with Datasette (publish data) a little easier, but it also makes the first steps beyond the standard thing Datasette seem a little harder, since you have to start to deal with the things that Datasette has been handling for you.

As you can see here, Datasette’s app contains add_route invocations that are directly analogous to the app.get paragraphs that I attempted to point to.

I think you could go in any of these directions, but I don’t think I can tell you which one would be easiest for you - and none of them are particularly easy:

  • You could stop using Datasette, and write the functionality you really want in Node (which is built on libuv, just like Python’s uvicorn) and use technologies like Sqlite and Vega directly, rather than via Datasette.
  • Keep using Datasette, but put a proxy in front of it, which would handle some routes (like /refresh) and forward some other routes.
  • Load Datasette as a Python module instead of invoking it on the command line. This would mean doing web development, in Python, on Glitch, and delegating to Datasette for some functionality, but delegating using in-process Python method calls, not inter-process http requests.

You might look at ~congruous-ermine or ~ten-sherbert for starting points if you wanted to pursue the first option.

Hope this helps,

Johnicholas

1 Like