ShellSpells
  • 🧙‍♂️Welcome!
    • ShellSpells
    • FAQs
    • License & Disclaimer
  • 🐧Linux
    • System Ops
      • Transcripts
      • Help
      • System Info
        • Date/Time
        • System Details
        • Patches & Updates
        • Init System Identification
        • Hostname / Host ID
        • Variables
        • Hardware & Resources
      • Filesystem
        • Traverse & Enumerate
        • Drives & Partitions
        • Shares
        • Packages
        • Connected Devices
        • Kernel Modules (Drivers)
      • Users & Groups
        • Enumerate
        • Modify
      • Network
        • Enumerate
        • Modify
      • Scheduled Jobs
        • Enumerate
        • Modify
      • Processes
        • Enumerate
        • Modify
        • Custom Script and Shared Object
        • Process I/O Redirection
      • Services
        • Enumerate
        • Modify
        • Create a Service
      • Startup/Boot Scripts
        • Enumerate
        • Modify
      • Security
        • Antivirus
        • Firewall
        • SSH Keys
      • History & Logs
        • History
        • Logs
    • File Ops
      • Search
        • Filename
        • Content
        • Users (Owners)
        • Time
        • Size
        • Permission
        • Hidden Files
        • Inode
        • Find + Exec
        • Notes
      • Enumerate Metadata
      • Modify Metadata
      • Read Content
      • Modify Content
      • Extract Content
      • Sort / Compare / Count
      • Move
      • Copy
      • Execute
      • Hash
      • Encode/Decode
      • Compress/Decompress
      • Working With Weird Filenames
    • Terminal Ops
      • Keyboard Shortcuts
      • Tmux Shortcuts
  • 🪟Windows
    • System Ops
      • Transcripts
      • Help
      • System Info
        • One-liners
        • Date/Time
        • System Details
        • Hotfixes
        • Domain or Workgroup
        • Data Execution Prevention
        • Variables
        • Hardware & Resources
      • Filesystem
        • Traverse & Enumerate
        • Drives & Partitions
        • Installed Software
        • Drivers
        • Shares
      • Registry
        • Enumerate
        • Modify
        • Forensically Relevant Keys
      • Users & Groups
        • Enumerate
        • Modify
      • Network
        • Enumerate
        • Modify
      • Scheduled Tasks
      • Processes
        • Enumerate
        • Modify
      • Services
        • Enumerate
        • Modify
      • Autorun / Startup
        • Enumerate
        • Modify
      • Security
        • Permissions
          • Enumerate
          • Page
        • Antivirus
        • Firewall
          • Enumerate
          • Modify
        • Audit Policies
        • Remoting
          • Enumerate
          • Modify
          • Registry Locations
        • Stored Credentials
      • Remote Command Execution
      • Active Directory
        • Enumerate
        • Modify
      • History & Logs
        • History
        • Logs
      • PowerShell Config
      • Scripting
      • WMIC Notes
    • File Ops
      • Search
        • Filename
        • Time
        • Size
        • Permissions
        • Attributes
        • Wildcarding
      • Enumerate Metadata
        • One Liners
        • Users (Owners)
        • Timestamps
        • Size
        • Permissions
        • Attributes
      • Modify Metadata
        • Change Owner
        • Timestamps
        • Size
        • Attributes
      • Read Content
      • Modify Content
        • Overwrite
        • Insert
        • Append
        • Replace / Remove
        • Convert Case
        • Alternate Data Streams
      • Extract Content
      • Sort / Compare / Count
        • Sort
        • Count
        • Compare
      • Move
      • Copy
      • Execute
      • Hash
      • Encode/Decode
      • Compress/Decompress
      • Working With Weird Filenames
      • Output Formatting / Filtering
      • File Formatting
      • Operators
  • ⛓️Network
    • Traffic Manipulation
      • iptables
        • Option List
        • General Commands
        • Filter Tables
        • NAT
        • Mangle
        • Filter for SSH Traffic (Example)
      • nftables
    • Packet Capture
      • Syntax
      • TCPDump Examples
    • Packet Analysis
      • Wireshark
  • 🚗Maneuver
    • SSH
    • Control Sockets
    • RDP
    • Windows Port Proxy
  • 🛩️Data Transfer
    • SCP
    • FTP
    • Netcat
      • Netcat Relays
    • Server Interactions
    • Alternate Methods
  • 🪄REGEX
    • Examples
Powered by GitBook
On this page
  • Writing SysV Init Scripts
  • Step 1: Create the Script
  • Step 2: Implement Start, Stop, and Other Actions
  • Step 3: Make the Script Executable
  • Step 4: Test Your Script
  • Step 5: Enable the Service for Automatic Startup (Optional)
  • Writing SystemD Scripts
  • Step 1: Create the Service Unit File
  • Step 2: Save and Store the Unit File
  • Step 3: Reload systemd, Enable and Start the Service
  • Step 4: Verify the Service Status
  • Writing Upstart Scripts

Was this helpful?

  1. Linux
  2. System Ops
  3. Startup/Boot Scripts

Modify

Writing SysV Init Scripts

Step 1: Create the Script

Open your text editor and create a new file. For this example, let's call it myservice. Start with the shebang line to specify the script interpreter, usually /bin/bash or /bin/sh: Add a description, using comments, about the service and how to use the script.

Step 2: Implement Start, Stop, and Other Actions

SysV init scripts must handle at least the start, stop, and restart actions. It's also good practice to include status and potentially reload if applicable.

Here's a simple template:

### BEGIN INIT INFO
# Provides:          myservice
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       A simple script to manage myservice
### END INIT INFO

case "$1" in
    start)
        echo "Starting myservice"
        # Command to start your service, e.g.:
        /path/to/myservice start
        ;;
    stop)
        echo "Stopping myservice"
        # Command to stop your service, e.g.:
        /path/to/myservice stop
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    status)
        # Command to check status of your service, e.g.:
        /path/to/myservice status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0

Step 3: Make the Script Executable

Save your script in /etc/init.d/myservice (replace myservice with your service's name). Make it executable by running: sudo chmod +x /etc/init.d/myservice

Step 4: Test Your Script

Test your script to ensure it properly starts, stops, restarts, and reports the status of your service: sudo /etc/init.d/myservice start sudo /etc/init.d/myservice stop sudo /etc/init.d/myservice restart sudo /etc/init.d/myservice status

Step 5: Enable the Service for Automatic Startup (Optional)

If you want your service to start automatically at boot, you need to add symbolic links to your script in the appropriate /etc/rc?.d/ directories. You can use the update-rc.d command for this purpose: sudo update-rc.d myservice defaults

This command will create the necessary links based on the Default-Start and Default-Stop values specified in the init script header.

Note:

The ### BEGIN INIT INFO section at the top of the script provides metadata for dependency-based boot sequencing. Adjust Required-Start, Required-Stop, Default-Start, and Default-Stop as needed for your service.

Remember to replace /path/to/myservice with the actual command or script used to start, stop, and check the status of your service. This basic template should be customized to fit the specific needs of the service you're managing.

Writing SystemD Scripts

Step 1: Create the Service Unit File

Open your text editor to create a new file with the .service extension. For this example, we'll name the file myservice.service. Define the service unit by specifying directives under [Unit], [Service], and [Install] sections.

Here's a basic template:

[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
ExecStart=/path/to/your/application
Restart=on-failure

[Install]
WantedBy=multi-user.target

Description- gives a human-readable description of the service. After- specifies the order in which services are started. network.target is common for services that require network access. Type- defines the startup behavior of the service. simple is the default, where systemd considers the service started immediately after the ExecStart process is forked. ExecStart- specifies the command to start the service. Replace /path/to/your/application with the actual command. Restart- defines the service restart policy. on-failure means the service will be restarted when it exits with a non-zero exit code. WantedBy- determines where the service is installed if enabled. multi-user.target is a common target for services that should start in a multi-user system runlevel.

Step 2: Save and Store the Unit File

Save your file with the .service extension. Store the unit file in /etc/systemd/system/. The path for the unit file will be /etc/systemd/system/myservice.service. You might need root permissions to save files in this directory. sudo cp myservice.service /etc/systemd/system/myservice.service

Step 3: Reload systemd, Enable and Start the Service

After saving your service file, you need to tell systemd about the new service and optionally enable it to start at boot.

Reload systemd to read the new service file:

sudo systemctl daemon-reload

Enable the service to start on boot:

sudo systemctl enable myservice.service

Start the service now without rebooting:

sudo systemctl start myservice.service

Step 4: Verify the Service Status

You can check the status of your service to ensure it's running correctly:

sudo systemctl status myservice.service

Additional Notes

Modify the ExecStart, and potentially add ExecStop and ExecReload, to manage how your service starts, stops, and reloads. Adjust the [Unit] section to specify dependencies and ordering to other units with Requires, Wants, and Before or After. For complex services, you might also need to set environment variables using Environment or EnvironmentFile directives in the [Service] section.

Writing Upstart Scripts

When writing Upstart job files, you define jobs using stanzas that specify how and when a job should start or stop. These stanzas include:

start on Specifies the event that causes the job to start. stop on Specifies the event that causes the job to stop. script ... end script or exec: Used to define the command or script to run.

Example of starting a service:

# /etc/init/myservice.conf
description "My Service"
start on filesystem and started networking
stop on shutdown

exec /usr/local/bin/myservice

This configuration starts myservice when the filesystem and networking are ready and stops it on system shutdown. The exec stanza directly runs the command specified.

PreviousEnumerateNextSecurity

Last updated 1 year ago

Was this helpful?

🐧