2
0

.automated_script.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env bash
  2. script_cmdline() {
  3. local param
  4. for param in $(</proc/cmdline); do
  5. case "${param}" in
  6. script=*)
  7. echo "${param#*=}"
  8. return 0
  9. ;;
  10. esac
  11. done
  12. }
  13. automated_script() {
  14. local script rt
  15. script="$(script_cmdline)"
  16. if [[ -n "${script}" && ! -x /tmp/startup_script ]]; then
  17. if [[ "${script}" =~ ^((http|https|ftp|tftp)://) ]]; then
  18. # there's no synchronization for network availability before executing this script
  19. printf '%s: waiting for network-online.target\n' "$0"
  20. until systemctl --quiet is-active network-online.target; do
  21. sleep 1
  22. done
  23. printf '%s: downloading %s\n' "$0" "${script}"
  24. curl "${script}" --location --retry-connrefused --retry 10 --fail -s -o /tmp/startup_script
  25. rt=$?
  26. else
  27. cp "${script}" /tmp/startup_script
  28. rt=$?
  29. fi
  30. if [[ ${rt} -eq 0 ]]; then
  31. chmod +x /tmp/startup_script
  32. printf '%s: executing automated script\n' "$0"
  33. # note that script is executed when other services (like pacman-init) may be still in progress, please
  34. # synchronize to "systemctl is-system-running --wait" when your script depends on other services
  35. /tmp/startup_script
  36. fi
  37. fi
  38. }
  39. if [[ $(tty) == "/dev/tty1" ]]; then
  40. automated_script
  41. fi