@fnet/service
v0.1.4
Published
This project provides a straightforward utility for managing system services across different operating systems, including Windows, macOS, and Linux. The primary aim is to facilitate the process of registering, unregistering, starting, and stopping servic
Downloads
261
Readme
@fnet/service
This project provides a straightforward utility for managing system services across different operating systems, including Windows, macOS, and Linux. The primary aim is to facilitate the process of registering, unregistering, starting, and stopping services on these platforms using simple commands. With this utility, users can manage their services without needing to delve deeply into the specific service management tools of each OS.
How It Works
This utility functions by leveraging platform-specific commands to handle services. The user provides details such as the service name, description, command to execute, and environment variables. Based on the specified action, the utility executes the appropriate commands to either register, unregister, start, or stop a service. It automatically determines the platform you are on and applies the necessary operations for that system.
Key Features
- Cross-Platform Service Management: Supports Windows, macOS, and Linux, ensuring compatibility and ease of use across all major operating systems.
- Service Registration/Unregistration: Easily create and remove services from system service managers.
- Start/Stop Services: Control service start and stop actions with straightforward commands.
- Environment Variables Support: Allows you to specify environment variables for your services, adaptable for various execution contexts.
Conclusion
This utility serves as a helpful tool for those needing to manage system services across different operating systems. By simplifying the process of handling service operations, users can save time and reduce complexity, making it a convenient addition to any development or IT management toolkit.
@fnet/service Developer Guide
Overview
The @fnet/service
library provides a straightforward interface for managing operating system services across different platforms: Windows, macOS, and Linux. It allows developers to register, unregister, start, stop, and enable services. This can be especially useful for automating service management tasks in environments where applications require consistent setup and execution as a background process.
Installation
You can install the @fnet/service
library using npm or yarn. Below are the commands for installation:
Using npm:
npm install @fnet/service
Using yarn:
yarn add @fnet/service
Usage
The primary export of the library is a function that allows you to control services. The function requires specific parameters that define the action and service properties.
Parameters:
action
: The action to be performed (register
,unregister
,start
,stop
,enable
).name
: The name of the service.description
: A brief description of the service.command
: An array of command-line arguments to run the service.user
: The user under whose account the service will run (optional).env
: An object defining environment variables for the service (optional).working_dir
: The working directory for the service (optional).
Example Usages
Registering a new service:
import manageService from '@fnet/service';
manageService({
action: 'register',
name: 'MyService',
description: 'A demo service',
command: ['node', '/path/to/app.js'],
user: 'serviceUser', // Optional
env: { NODE_ENV: 'production' }, // Optional
working_dir: '/path/to/working/directory' // Optional
});
Starting a registered service:
manageService({
action: 'start',
name: 'MyService'
});
Unregistering a service:
manageService({
action: 'unregister',
name: 'MyService'
});
Examples
Here's a basic setup to create and manage a service on any supported platform:
Register and Start a Service
import manageService from '@fnet/service';
// Register a new service
manageService({
action: 'register',
name: 'AutoBackup',
description: 'Automated Backup Service',
command: ['node', '/scripts/backup.js']
});
// Start the service
manageService({ action: 'start', name: 'AutoBackup' });
Stop and Unregister a Service
import manageService from '@fnet/service';
// Stop a running service
manageService({ action: 'stop', name: 'AutoBackup' });
// Unregister the service
manageService({ action: 'unregister', name: 'AutoBackup' });
These examples cover the essential operations like registering, starting, stopping, and unregistering a service. Modify the parameters according to your application's needs to ensure it fits within your system's service management framework.
Acknowledgement
The @fnet/service
library utilizes the Node.js Child Process module, as part of its functionality to execute command-line operations across different operating systems. These are foundational tools within the Node.js ecosystem.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
action:
type: string
description: The action to be performed on the service (register, unregister,
start, stop)
enum:
- register
- unregister
- start
- stop
name:
type: string
description: The name of the service
description:
type: string
description: A brief description of the service
command:
type: array
description: The command and arguments that start the service, as an array of strings
items:
type: string
user:
type: string
description: User under which the service should run
env:
type: object
description: Environment variables for the service
additionalProperties:
type: string
working_dir:
type: string
description: The working directory for the service
required:
- action
- name
if:
properties:
action:
const: register
then:
required:
- description
- command