I have a bash script, which im using on a handful machines. On all machines, its the same script. Sometimes i need to adjust/update the script. With that, i need to copy the script to all this machines again.

Is there some way to update the script only on one central location and if this is done, the script checks itself, if there is a updated/changed variant on this central location and if so, copy the new version and restart itself after copy?

  • moonpiedumplings@programming.dev
    link
    fedilink
    English
    arrow-up
    3
    ·
    1 day ago

    Simple solution:

    Script is hosted on git somewhere. (Doesn’t have to be public, you can also pretty easily make git run over a small http or ssh solution). Or you can host a small http server or whatever.

    Script runs git pull (or curl or etc). Sets an env variable to declare that git pull has been ran. Then reruns itself. The script sees the env variable and skips the pull/download/update + rerun phase, and carries on.

    Also worth looking into, although they are probably overkill:

    Ansible, chef, or similar configuration management tools. Chef mostly follows a pull based model. You have a central config somewhere and it pulls it onto machines.

    Ansible mostly follows a push based model. You write a “playbook” on your local machine and it is executed on remote (maybe more than one remote machine) over ssh. Ansible is cool because the only thing you need on remote machines is python, which is usually preinstalled.

    Both can be inverted however, to do push/pull instead but that’a how they work by default.

  • KianaTabion@lemmy.today
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    1 day ago

    Syncthing helps with syncing the bash script across all of your devices. However, as for restarting itself after the update/change, you might want to setup a daemon/service (or something) that restarts the bash script whenever Syncthing does its bidding.

    • MoLoPoLY@discuss.tchncs.deOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 day ago

      Syncthing is blocked, or better the ports are blocked on 3 of the hosts (and i cant open them). I can use scp, to copy files. Ports 22 and 443 are the only ports, i can use on all hosts. Additionally, i cant install new software there.

      For the restart, i have found the line exec "$(realpath $0)" "$@". Using the script directly works with that. But since the script is called from a systemd service unit, i don’t know if this break the logic. The service unit is from type oneshot and calls the script multiple times (but different parameters), like this:

      ExecStart=/home/username/.local/bin/script.sh variant1
      ExecStart=/home/username/.local/bin/script.sh variant2
      ExecStart=/home/username/.local/bin/script.sh variant3
      

      script.sh does different things, when changing the first parameter. It starts the other variants, when the current variant finishes. Now i don’t know, if restarting /home/username/.local/bin/script.sh variant1 will break the logic and the other variants will either not executed or in some unexpected schedule.

  • lordnikon@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 day ago

    The way to do this would be use got have a repo on one machine and do a git pull on the other machines on a cron