Skip to content
Snippets Groups Projects
Commit 0f58c1bc authored by Recolic's avatar Recolic :house_with_garden:
Browse files

.allow lc_bgrun every 3h, update example, bump version

parent 19f5dcb7
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,3 @@ This project is to demonstrate a possibility to manage customization in a centra ...@@ -19,7 +19,3 @@ This project is to demonstrate a possibility to manage customization in a centra
## dependency ## dependency
coreutils bash grep sudo coreutils bash grep sudo
## TODO
init order? what is one init depends on another?
# we run this master config only once, for each "registered" user. # we run this master config only once, for each "registered" user.
# To use linuxconf: # To use linuxconf:
# sudo linuxconf register () # sudo linuxconf register path/to/masterconf.sh
# linuxconf register () # sudo -u ... linuxconf register path/to/masterconf.sh
# Common use: # Common use:
# 1. install archlinux fresh # 1. install fresh archlinux
# 2. copy this linuxconf dir, please it somewhere. (put a linuxconf wrapper script in this dir? automatically install linuxconf from githubi (pkg manager if available) if not installed, then run real linuxconf.) # 2. download this directory from somewhere
# 3. ./linuxconf register . # 3. ./linuxconf.wrapper register ./masterconf.sh
# > check if linuxconf installed, install it if not. # > linuxconf would be downloaded & installed automatically. lc_init() will happen right now.
# > create / overwrite config file in /etc/linuxconf.conf, just 1 line: `root=/path/to/...`
# don't touch it if already have correct content, report error if need update & no permission
# > conf.d/basic.sh should install pkgs, (possibly) create new user, (possibly) run `linuxconf register .` again with new user.
#
# order by filename # order by filename
lc_include conf.d/* lc_include conf.d/*
# If you have multiple user.. You might need this in your config file. # If you have multiple user but this file is for a specific user...
lc_assert_user_is root lc_assert_user_is root
lc_assert_user_is_not root # lc_assert_user_is_not root
# I strongly suggest u design lc_init() as an idempotent operation. Just in case u accidentally run `linuxconf register` elsewhere. # I strongly suggest u design lc_init() as an idempotent operation. Just in case u accidentally run `linuxconf register` elsewhere.
lc_init () { lc_init () {
# as root / as user?
# Some helpful functions.. To stop running current script: # Some helpful functions.. To stop running current script:
[ -f /usr/bin/apt ] || die "This is not even ubuntu. Stop!" [ -f /usr/bin/apt ] || lc_die "This is not even ubuntu. Stop!"
# To print an error message: # To print an error message:
apt install some_package || err "Failed to install my favorite pkg!" apt install some_package || lc_err "Failed to install my favorite pkg!"
gsettings set var=123
} }
lc_startup () { lc_startup () {
# as root / as user? sysctl kernel.sysrq=1
# You want desktop env? Use lc_login()
# want some daemon in background? Note: linuxconf dir is always your workdir.
# want some daemon in background?
lc_bgrun /tmp/server.log my_server --arg1 123 --arg2 "hello world !" lc_bgrun /tmp/server.log my_server --arg1 123 --arg2 "hello world !"
# Guaranteed! linuxconf dir is your current workdir. # want it auto_restart?
lc_bgrun /dev/null auto_restart important_service --config files/test.conf lc_bgrun /dev/null auto_restart important_service --config files/test.conf
# want some timely task?
lc_bgrun /dev/null every 1h curl https://example-ddns.com/api
} }
lc_login () { lc_login () {
# Your desktop environment must implement "XDG autostart". Ref: https://wiki.archlinux.org/title/XDG_Autostart # Your desktop environment must implement "XDG autostart". Ref: https://wiki.archlinux.org/title/XDG_Autostart
# Otherwise... Use lc_init to your task into wherever u'd like.
# #
# Warning: Could be called multiple times if user logout/login again. # Warning: Could be called multiple times if user logout/login again.
# Desktop environment available.
echo pass | gnome_keyring_unlock
} }
# watch out! does your user have access to target directory? # watch out! does your user have access to target directory?
# okay to stay inside lc_init() or outside, okay if files/vimrc doesn't exist yet. # lc_fsmap works inside lc_xxx() or outside, also works even if files/vimrc doesn't exist yet.
lc_fsmap files/vimrc $HOME/.vimrc lc_fsmap files/vimrc $HOME/.vimrc
...@@ -15,18 +15,24 @@ function lc_die () { ...@@ -15,18 +15,24 @@ function lc_die () {
function lc_bgrun () { function lc_bgrun () {
# Usage: lc_bgrun /var/log/your.log sslocal -s xxx -p 'hello world my password' -l xxx --xxx # Usage: lc_bgrun /var/log/your.log sslocal -s xxx -p 'hello world my password' -l xxx --xxx
# Usage: lc_bgrun /var/log/your.log auto_restart frpc -c my_server.ini # Usage: lc_bgrun /var/log/your.log auto_restart frpc -c my_server.ini
# v202504-1 # Usage: lc_bgrun /dev/null every 1h curl https://example-ddns.com/api
# v202504-2
local logF="$1" local logF="$1"
shift shift
local cmd="$1" local cmd="$1"
shift shift
local t=1
if [ "$cmd" = every ]; then
t="$1"
cmd=auto_restart
fi
if [ "$cmd" = auto_restart ] && [ ! -f /tmp/.auto_restart ]; then if [ "$cmd" = auto_restart ] && [ ! -f /tmp/.auto_restart ]; then
echo IyEvYmluL2Jhc2gKCndoaWxlIHRydWU7IGRvCiAgICAiJEAiCiAgICBbWyAkPyA9IDEzMCBdXSAmJiBicmVhawogICAgc2xlZXAgMQpkb25lCg== | base64 -d > /tmp/.auto_restart echo IyEvYmluL2Jhc2gKd2hpbGUgdHJ1ZTsgZG8KIiRAIgpbWyAkPyA9IDEzMCBdXSAmJiBicmVhawpzbGVlcCAiJHQiCmRvbmUK | base64 -d > /tmp/.auto_restart
chmod ugo+rx /tmp/.auto_restart chmod ugo+rx /tmp/.auto_restart
cmd=/tmp/.auto_restart cmd=/tmp/.auto_restart
fi fi
echo "[$(date --utc)] EXEC $cmd $@" >> "$logF" echo "[$(date --utc)] EXEC $cmd $@" >> "$logF"
nohup "$cmd" "$@" >> "$logF" 2>&1 & disown t="$t" nohup "$cmd" "$@" >> "$logF" 2>&1 & disown
return $? return $?
} }
...@@ -245,7 +251,7 @@ function lci_install_login_hook () { ...@@ -245,7 +251,7 @@ function lci_install_login_hook () {
fi fi
} }
lci_version=0.2.2 lci_version=0.2.3
subcommand="$1" subcommand="$1"
if [[ "$subcommand" != register ]] && [[ "$subcommand" != "" ]]; then if [[ "$subcommand" != register ]] && [[ "$subcommand" != "" ]]; then
[[ ! -f /etc/linuxconf.conf ]] && lc_die "Please run '$0 register <path/to/masterconf.sh>' at least once" [[ ! -f /etc/linuxconf.conf ]] && lc_die "Please run '$0 register <path/to/masterconf.sh>' at least once"
...@@ -258,9 +264,6 @@ if [[ "$subcommand" = register ]]; then ...@@ -258,9 +264,6 @@ if [[ "$subcommand" = register ]]; then
lci_install_login_hook || lc_die "failed to install on_login hook" lci_install_login_hook || lc_die "failed to install on_login hook"
lci_init_if_needed lci_init_if_needed
lci_startup_if_needed lci_startup_if_needed
elif [[ "$subcommand" = _cron ]]; then
# TODO: implement cron. with crontab or same systemd service?
:
elif [[ "$subcommand" = _startup ]]; then elif [[ "$subcommand" = _startup ]]; then
lci_startup_if_needed lci_startup_if_needed
elif [[ "$subcommand" = _startup_all ]]; then elif [[ "$subcommand" = _startup_all ]]; then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment