Ubuntu虚拟机中创建Randn systemd服务启动失败:Exec格式错误求助
Fixing "Exec format error" for your Randn.service
Hey there! Let's break down what's causing that Exec format error and get your random number service up and running. Most of the issues are in your .service file syntax, plus a couple of quick tweaks to your script.
1. Fix critical syntax errors in Randn.service
Your service file has a few syntax mistakes that are breaking systemd's ability to load it:
- [Unit] section
Afterline: You wroteAfter network.target = auditd.service—this is invalid. The correct syntax is to put the=immediately after the directive name, no spaces, then list your dependencies:After=network.target auditd.service - [Service] section
ExecStart/ExecStoppaths: You have extra spaces and incorrect formatting here. If yourrandn.shscript is located at/usr/local/bin/randn.sh, the lines should be:
(Note: Only includeExecStart=/usr/local/bin/randn.sh start ExecStop=/usr/local/bin/randn.sh stopstart/stopif your script actually accepts those as command-line arguments. If it's a simple script that just generates a random number, you can omit the arguments entirely.)
2. Make your randn.sh script executable and properly formatted
Even if your .service file is fixed, systemd will throw an exec error if your script isn't set up correctly:
- Add a shebang at the top: The first line of
randn.shmust tell the system which interpreter to use. Add this:#!/bin/bash - Give it execute permissions: Run this command to make the script executable:
chmod +x /usr/local/bin/randn.sh - Check your script logic: Since you're using
Type=simpleandRestart=always, if your script just generates a random number and exits, the service will restart non-stop. If you want it to run continuously, add a loop (e.g.,while true; do echo $((1 + RANDOM % 20)); sleep 5; doneto generate a number every 5 seconds).
3. Verify and reload the service
Once you've made these changes:
- Check for remaining syntax errors in your service file:
systemd-analyze verify Randn.service - Reload systemd to pick up the corrected service file:
systemctl daemon-reload - Try starting the service again:
systemctl start randn.service
You can check if it's running with systemctl status randn.service—that will show you any remaining errors if something's still off.
内容的提问来源于stack exchange,提问作者Akarsh
相关产品推荐
相关产品推荐

