0

I'm trying to create a config file for my unit but when I start the daemon after enabling and shutdown the computer it doesnt works. The script only executes the ExecStart line after network.target on boot. Edit: Code:

[Unit]
Description="Service that executes Discord Bot at Boot and Shutdown."
After=network.target
Before=poweroff.target
[Service]
ExecStart=/usr/bin/mi_script_bash.sh start
ExecStop=/usr/bin/mi_script_bash.sh stop
Type=oneshot
RemainAfterExit=true
[Install]
WantedBy= default.target poweroff.target

The ExecStart and ExecStop Calls are for the bash script mi_script_bash.sh (simple string comparison for calling to a NodeJs script):

#!/bin/bash

[ "$1" == "start" ] && node /home/ser356/Escritorio/discordjs/app.js start

[ "$1" == "stop" ] && node /home/ser356/Escritorio/discordjs/app.js stop

And then the node script:

const {Client,Intents} = require("discord.js");
require('dotenv').config();
var hora=new Date();
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); 
var argumentos=process.argv[2];
console.log(argumentos)
//client.login("OTUwNTQyNDQ5MzYzODA4MzA2.YiabpA.bulmNmwGgz9s8kOPyvjngR_rSbo");



client.on('ready', () => {
var canal =  client.channels.cache.get("950644612517724171")
if(argumentos==='start'){
canal.send("Mi dueño me ha arrancado "+hora);
}

else if(argumentos==='stop'){
canal.send("Mi dueño me requiere "+hora);
}

else{
canal.send("nada")

}
});




 client.login('token')

1 Answer 1

0

Finally reached a solution working for me.

Dividing the Unit in two daemons and giving chmod 0644 permissions for each one

servicio_discord.service :

[Unit]
Description="Service that executes Discord Bot at Boot."
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/mi_script_bash.sh start
[Install]
WantedBy=multi-user.target

Where the service IS executed After the Network service, WantedBy=multi-user.target indicates the services to run on multi-user state Type=simple indicates the daemon to run on the foreground

Equally:

servicio_discord_end.service

[Unit]
Description="Run Discord Bot at shutdown"
DefaultDependencies=no
Before=shutdown.target
[Service]
ExecStart=/usr/bin/mi_script_bash.sh stop
[Install]
WantedBy=shutdown.target

In this case DefaultDependencies is no because we have Before option but not an after option as it would be expected by default.

See: https://github.com/systemd/systemd/issues/7113#issuecomment-337451441

Then running:

systemctl daemon-reload
systemctl enable sistema*
systemctl start sistema*
systemctl status sistema* #optional
reboot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .