How to make sure an application keeps running on Linux

前端 未结 16 1355
清歌不尽
清歌不尽 2020-11-28 18:51

I\'m trying to ensure a script remains running on a development server. It collates stats and provides a web service so it\'s supposed to persist, yet a few times a day, it

16条回答
  •  半阙折子戏
    2020-11-28 19:21

    If you are using a systemd-based distro such as Fedora and recent Ubuntu releases, you can use systemd's "Restart" capability for services. It can be setup as a system service or as a user service if it needs to be managed by, and run as, a particular user, which is more likely the case in OP's particular situation.

    The Restart option takes one of no, on-success, on-failure, on-abnormal, on-watchdog, on-abort, or always.

    To run it as a user, simply place a file like the following into ~/.config/systemd/user/something.service:

    [Unit]
    Description=Something
    
    [Service]
    ExecStart=/path/to/something
    Restart=on-failure
    
    [Install]
    WantedBy=graphical.target
    

    then:

    systemctl --user daemon-reload
    systemctl --user [status|start|stop|restart] something
    

    No root privilege / modification of system files needed, no cron jobs needed, nothing to install, flexible as hell (see all the related service options in the documentation).

    See also https://wiki.archlinux.org/index.php/Systemd/User for more information about using the per-user systemd instance.

提交回复
热议问题