You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 After line: You wrote After 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/ExecStop paths: You have extra spaces and incorrect formatting here. If your randn.sh script is located at /usr/local/bin/randn.sh, the lines should be:
    ExecStart=/usr/local/bin/randn.sh start
    ExecStop=/usr/local/bin/randn.sh stop
    
    (Note: Only include start/stop if 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.sh must 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=simple and Restart=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; done to generate a number every 5 seconds).

3. Verify and reload the service

Once you've made these changes:

  1. Check for remaining syntax errors in your service file:
    systemd-analyze verify Randn.service
    
  2. Reload systemd to pick up the corrected service file:
    systemctl daemon-reload
    
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:25:10