From 0f58c1bc2f9382118ed8c7b355658f297be74910 Mon Sep 17 00:00:00 2001
From: Recolic <git@me.recolic.net>
Date: Sat, 5 Apr 2025 16:31:00 -0700
Subject: [PATCH] .allow lc_bgrun every 3h, update example, bump version

---
 README.md                       |  4 ---
 examples/template/masterconf.sh | 44 ++++++++++++++-------------------
 linuxconf                       | 17 +++++++------
 3 files changed, 29 insertions(+), 36 deletions(-)

diff --git a/README.md b/README.md
index 9e63ac4..9dd4057 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,3 @@ This project is to demonstrate a possibility to manage customization in a centra
 ## dependency
 
 coreutils bash grep sudo
-
-## TODO
-
-init order? what is one init depends on another?
diff --git a/examples/template/masterconf.sh b/examples/template/masterconf.sh
index 1b282a7..c373c72 100644
--- a/examples/template/masterconf.sh
+++ b/examples/template/masterconf.sh
@@ -1,54 +1,48 @@
 # we run this master config only once, for each "registered" user.
 # To use linuxconf:
-#   sudo linuxconf register ()
-#   linuxconf register ()
+#   sudo linuxconf register path/to/masterconf.sh
+#   sudo -u ... linuxconf register path/to/masterconf.sh
 # Common use:
-# 1. install archlinux fresh
-# 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.)
-# 3. ./linuxconf register .
-#      > check if linuxconf installed, install it if not.
-#      > 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.
-#
+# 1. install fresh archlinux
+# 2. download this directory from somewhere
+# 3. ./linuxconf.wrapper register ./masterconf.sh
+#      > linuxconf would be downloaded & installed automatically. lc_init() will happen right now.
 
 # order by filename
 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_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.
 lc_init () {
-    # as root / as user?
-
     # 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:
-    apt install some_package || err "Failed to install my favorite pkg!"
-
-    gsettings set var=123
+    apt install some_package || lc_err "Failed to install my favorite pkg!"
 }
 
 lc_startup () {
-    # as root / as user?
-    # You want desktop env? Use lc_login()
-    
-    # want some daemon in background?
+    sysctl kernel.sysrq=1
+
+    # want some daemon in background? Note: linuxconf dir is always your workdir.
     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
+    # want some timely task?
+    lc_bgrun /dev/null every 1h curl https://example-ddns.com/api
 }
 
 lc_login () {
     # 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.
+    # Desktop environment available.
+    echo pass | gnome_keyring_unlock
 }
 
 # 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
 
diff --git a/linuxconf b/linuxconf
index c76f136..3c9b4b3 100755
--- a/linuxconf
+++ b/linuxconf
@@ -15,18 +15,24 @@ function lc_die () {
 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 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"
     shift
     local cmd="$1"
     shift
+    local t=1
+    if [ "$cmd" = every ]; then
+        t="$1"
+        cmd=auto_restart
+    fi
     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
         cmd=/tmp/.auto_restart
     fi
     echo "[$(date --utc)] EXEC $cmd $@" >> "$logF"
-    nohup "$cmd" "$@" >> "$logF" 2>&1 & disown
+    t="$t" nohup "$cmd" "$@" >> "$logF" 2>&1 & disown
     return $?
 }
 
@@ -245,7 +251,7 @@ function lci_install_login_hook () {
     fi
 }
 
-lci_version=0.2.2
+lci_version=0.2.3
 subcommand="$1"
 if [[ "$subcommand" != register ]] && [[ "$subcommand" != "" ]]; then
     [[ ! -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
     lci_install_login_hook || lc_die "failed to install on_login hook"
     lci_init_if_needed
     lci_startup_if_needed
-elif [[ "$subcommand" = _cron ]]; then
-    # TODO: implement cron. with crontab or same systemd service?
-    :
 elif [[ "$subcommand" = _startup ]]; then
     lci_startup_if_needed
 elif [[ "$subcommand" = _startup_all ]]; then
-- 
GitLab