Yu1@HomoSapiens
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    Note: Create a Process Automation via linux systemd.service (system daemon) ================================================================ 筆者目前擔任之職位中(韌體測試工程師), 有一項目, 謂之"開關機測試" 簡單來說, 就是透過外部開關來控制主機on/off狀態, 再由主機內運行腳本運行測試項目和記錄開關機次數; 最後再和外部開關所搭載之計數器比較,觀察次數是否相符. 以往都將script寫入 **.bashrc**; 然而這次的OS環境沒有自動登入(boot-in後不會自動開啟bash shell)導致腳本失效. 所以改用建立daemon(systemd,系統服務)的手法,並加以記錄 Structure ------------------------------------------------------------------ 運行腳本的檔案結構如下: ![image](https://hackmd.io/_uploads/BJTSq9Qe1e.png) 其中"createSystemDaemon.sh"功能為建立daemon, 和產生自動指令腳本(/etc/rc.local) Details ------------------------------------------------------------------ "createSystemDaemon.sh"內容如下: ![image](https://hackmd.io/_uploads/rybfWoQl1g.png) 產生daemon configuration的具體方法見第16行: **經判斷, 如路徑下無rc-local.service文件, 就複製到路徑/etc/systemd/system/** rc-local.service中的內容: ![image](https://hackmd.io/_uploads/BkFDjdVxyg.png) ### [Unit] `Description`:描述daemon,表示它是為了相容`/etc/rc.local`的服務 `ConditionPathExists`:只有在`/etc/rc.local`存在時才會啟動daemon;如果這個檔案不存在則不執行 ### [Service] `Type=forking`:表示這是一個會產生子行程(fork)的服務,啟動後父行程會退出,子行程在背景執行 `ExecStart`:daemon主要執行的內容;其中的`/etc/rc.local start`,表示執行該script中寫入的指令 `TimeoutSec=0`:無超時時間,也就是`systemd`會等待它完成。 `StandardOutput=tty`:指定標準輸出為TTY(電傳打字機,粗淺的說即是一般終端機文字界面),方便除錯時檢視狀態 `RemainAfterExit=yes`:"保持存在":`rc.local`執行完畢且退出,systemd仍會將此daemon標記為「仍在執行」,確保它不會被重啟或標記為失敗。 ### [Install] `WantedBy=multi-user.target`:這表示當系統進入"多重使用者狀態"(可以理解為[執行級別3](https://zh.wikipedia.org/zh-tw/%E8%BF%90%E8%A1%8C%E7%BA%A7%E5%88%AB))時會啟用此daemon 接著啟用daemon: `systemctl enable rc-local.service` 確保**systemd-service**建立之後, 再建立設定檔: **rc.local**; 詳見第21~24行: `touch /etc/rc.local` `echo \#\!\/bin\/bash >> /etc/rc.local` 最後, 將 **開機自動執行程序**加入 **rc.local**中, 並給予權限 範例: ![image](https://hackmd.io/_uploads/BJEAGjQgkg.png) Using command & check status ------------------------------------------------------------------ 將整個資料夾複製到裝置後,直接執行`createSystemDaemon.sh`即可 Command: `./createSystemDaemon.sh` 進入系統後可使用以下指令檢視運行狀態 Command: `systemctl status rc-local.service` ![image](https://hackmd.io/_uploads/SkyT4sQe1l.png) Ref. ------------------------------------------------------------------ [系統服務管理基礎教學與範例](https://blog.gtwang.org/linux/linux-basic-systemctl-systemd-service-unit-tutorial-examples/) [Debian / Ubuntu 開機自動執行 Shell Script](https://www.ltsplus.com/linux/debian-ubuntu-shell-script-startup) [Shell Script 檢查檔案或目錄是否存在](https://www.ltsplus.com/linux/shell-script-check-file-dir-exists) [How to Check If a File Exists in Linux Bash Scripts](https://www.howtogeek.com/815684/bash-check-if-file-exists/) [create systemd service](https://cloud.tencent.com/developer/article/2384392) [about rc.local](https://blog.csdn.net/qq_43307934/article/details/130366433) [鳥哥:系統服務與開機流程](https://linux.vbird.org/linux_basic_train/centos7/unit13.php)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully