Restart a service
mikecr123
PROOP

a year ago

How can I restart a Railway service every 24 hours? Without using Docker, is there a way to make the "Restart" button only work every 24 hours?

$5 Bounty

8 Replies

eager4top
FREE

a year ago

I think you will manually trigger the restart button as when you so desire to do so.


sim
FREE

10 months ago

What’s the reason for restarting it every 24 hours? Railway has a crons feature that could do the trick: https://docs.railway.com/reference/cron-jobs


thetaautoworld
HOBBY

10 months ago

the cron jobs does not allow to key in command (kill 1), so how do I schedule my service to restart every 24 hours ?


clashing
FREE

10 months ago

With the default settings, won't the service would run non-stop, with no need of manually scaling up the servers?


thetaautoworld
HOBBY

10 months ago

I need to restart my service for like every 24 hours, it consume a lot of memory, so I want to restart the service to free up the memory, how do I setup in cron jobs ? it doesn't allow command


clashing
FREE

10 months ago

Mike & Autoworld, you can do this to resolve your issue:

a. Create a simple Express server that uses Railway's Public API to restart a desired deployment.

b. Add that as a service in your Railway account, and schedule it as a CRON job to run every 12 hours (0 */12 * * *)

The express file should look like this:

import express from "express";

import cors from "cors";

import fetch from "node-fetch";

const app = express();

app.use(cors({ origin: "*" }));

const RAILWAY_API_URL = "https://backboard.railway.com/graphql/v2";

const RAILWAY_API_TOKEN = process.env.RAILWAY_API_TOKEN;

const DEPLOYMENT_ID = process.env.DEPLOYMENT_ID;

app.get("/restart", async (req, res) => {

if (!DEPLOYMENT_ID) {

return res.status(400).json({ error: "DEPLOYMENT_ID is not set" });

}

try {

const response = await fetch(RAILWAY_API_URL, {

method: "POST",

headers: {

"Content-Type": "application/json",

Authorization: Bearer ${RAILWAY_API_TOKEN},

},

body: JSON.stringify({

query: `

mutation {

deploymentRestart(id: "${DEPLOYMENT_ID}")

}

`,

}),

});

const data = await response.json();

res.json(data);

} catch (error) {

res.status(500).json({ error: error.message });

}

});

let port = process.env.PORT || 7890;

app.listen(port, () => {

console.log(`Server running on port ${port}`);

});

To obtain the deployment ID via the Railway dashboard, you can open the service for which you need to restart via the express server. Then, using the URL, you can retrieve the deployment ID. For instance, in my case, you can see that the deployment ID for the service named "htoi" is de***, which is right after the "service/" keyword in the URL

Note: The API token that you need for this to work is either a PERSONAL/TEAM token, which can only be generated by the paid-plan users (which you can).

Required resources link:

https://docs.railway.com/reference/public-api

https://railway.com/account/tokens

That is a great way to solve your issue, and I hope you can use that. Do mark it as the solution if it helped you


clashing
FREE

10 months ago

Any update on this, mikecr123


clashing

Mike & Autoworld, you can do this to resolve your issue: a. Create a simple Express server that uses Railway's Public API to restart a desired deployment. b. Add that as a service in your Railway account, and schedule it as a CRON job to run every 12 hours (0 \*/12 \* \* \*) ![](https://station-server.railway.com/attachments/att_01k12dt6j2f6ba27z5dff9233e) The express file should look like this: > import express from "express"; > > import cors from "cors"; > > import fetch from "node-fetch"; > > const app = express(); > > app.use(cors({ origin: "\*" })); > > const RAILWAY\_API\_URL = "[https://backboard.railway.com/graphql/v2](https://backboard.railway.app/graphql/v2)"; > > const RAILWAY\_API\_TOKEN = process.env.RAILWAY\_API\_TOKEN; > > const DEPLOYMENT\_ID = process.env.DEPLOYMENT\_ID; > > app.get("/restart", async (req, res) => { > > if (!DEPLOYMENT\_ID) { > > return res.status(400).json({ error: "DEPLOYMENT\_ID is not set" }); > > } > > try { > > const response = await fetch(RAILWAY\_API\_URL, { > > method: "POST", > > headers: { > > "Content-Type": "application/json", > > Authorization: `Bearer ${RAILWAY_API_TOKEN}`, > > }, > > body: JSON.stringify({ > > query: \` > > mutation { > > deploymentRestart(id: "${DEPLOYMENT\_ID}") > > } > > \`, > > }), > > }); > > const data = await response.json(); > > res.json(data); > > } catch (error) { > > res.status(500).json({ error: error.message }); > > } > > }); > > let port = process.env.PORT || 7890; > > app.listen(port, () => { > > console.log(\`Server running on port ${port}\`); > > }); To obtain the deployment ID via the Railway dashboard, you can open the service for which you need to restart via the express server. Then, using the URL, you can retrieve the deployment ID. For instance, in my case, you can see that the deployment ID for the service named "htoi" is **de\*\*\*,** which is right after the "service/" keyword in the URL ![](https://station-server.railway.com/attachments/att_01k12e0t54f69awehwy73aejjc) Note: The API token that you need for this to work is **either a PERSONAL/TEAM** token, which can only be generated by the paid-plan users (which you can). Required resources link: <https://docs.railway.com/reference/public-api> <https://railway.com/account/tokens> That is a great way to solve your issue, and I hope you can use that. Do mark it as the solution if it helped you

clashing
FREE

10 months ago

Did that helped, mikecr123


Welcome!

Sign in to your Railway account to join the conversation.

Loading...