Improve service handler
This commit is contained in:
@@ -1,52 +1,58 @@
|
||||
import { exec } from 'child_process';
|
||||
|
||||
/**
|
||||
* Starts the given service from the command line.
|
||||
* @param {string} serviceName The service name to be started. Ex: ... start "{serviceName}.service"
|
||||
* @returns {Promise<void>}
|
||||
* Executes a system command with error handling.
|
||||
* @param {string} command The command to execute.
|
||||
* @returns {Promise<{ stdout: string, stderr: string }>} A promise resolving to an object containing stdout and stderr.
|
||||
*/
|
||||
export const startService = async (serviceName) => {
|
||||
const executeCommand = (command) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`sudo systemctl start ${serviceName}.service`, (error, stdout, stderr) => {
|
||||
exec(command, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
console.error(`Command failed with error: ${error.message}`);
|
||||
resolve({ stdout, stderr });
|
||||
} else {
|
||||
resolve();
|
||||
resolve({ stdout, stderr });
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Starts the given service from the command line.
|
||||
* @param {string} serviceName The service name to be started.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export const startService = async (serviceName) => {
|
||||
try {
|
||||
await executeCommand(`sudo systemctl start ${serviceName}.service`);
|
||||
} catch (error) {
|
||||
console.error(`Failed to start service: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Restarts the given service from the command line.
|
||||
* @param {string} serviceName The service name to be restarted. Ex: ... restart "{serviceName}.service"
|
||||
* @param {string} serviceName The service name to be restarted.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export const restartService = async (serviceName) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`sudo systemctl restart ${serviceName}.service`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
try {
|
||||
await executeCommand(`sudo systemctl restart ${serviceName}.service`);
|
||||
} catch (error) {
|
||||
console.error(`Failed to restart service: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Stops the given service from the command line.
|
||||
* @param {string} serviceName The service name to be stopped. Ex: ... stop "{serviceName}.service"
|
||||
* @param {string} serviceName The service name to be stopped.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export const stopService = async (serviceName) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`sudo systemctl stop ${serviceName}.service`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
try {
|
||||
await executeCommand(`sudo systemctl stop ${serviceName}.service`);
|
||||
} catch (error) {
|
||||
console.error(`Failed to stop service: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user