Views:
26,296β
Votes: 2β
Tags:
command-line
bash
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/948852
Title:
How can I wake a sleeping bash script?
ID:
/2017/08/23/How-can-I-wake-a-sleeping-bash-script_
Created:
August 23, 2017
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
I have a sleeping bash script started by cron
on boot. The script wakes every minute and sets the laptop display brightness based on sunrise and sunset obtained from the internet. A user configurable transition phase between full bright and full dim requires stepping up and stepping down values by 3, 4, 5 or whatever is calculated every minute.
Oli briefly touched on pstree
in his answer but rejected it because it would kill all sleep
instances. This can be avoided by narrowing the search using pstree options.
Using pstree -h
we see the entire heirarchy:
$ pstree -h
systemdββ¬βModemManagerββ¬β{gdbus}
β ββ{gmain}
ββNetworkManagerββ¬βdhclient
β ββdnsmasq
β ββ{gdbus}
β ββ{gmain}
ββaccounts-daemonββ¬β{gdbus}
β ββ{gmain}
ββacpid
ββagetty
ββatd
ββavahi-daemonβββavahi-daemon
ββcgmanager
ββcolordββ¬β{gdbus}
β ββ{gmain}
ββcronβββcronβββshβββdisplay-auto-brβββsleep
ββcups-browsedββ¬β{gdbus}
β ββ{gmain}
ββdbus-daemon
ββfwupdββ¬β3*[{GUsbEventThread}]
β ββ{fwupd}
β ββ{gdbus}
β ββ{gmain}
ββgnome-keyring-dββ¬β{gdbus}
β ββ{gmain}
β ββ{timer}
ββirqbalance
ββlightdmββ¬βXorgβββ3*[{Xorg}]
β ββlightdmββ¬βupstartββ¬βat-spi-bus-launββ¬βdbus-daemon
β β β β ββ{dconf worker}
β β β β ββ{gdbus}
β β β β ββ{gmain}
β β β ββat-spi2-registrββ¬β{gdbus}
β β β β ββ{gmain}
β β β ββbamfdaemonββ¬β{dconf worker}
β β β β ββ{gdbus}
β β β β ββ{gmain}
β β β ββchromeββ¬β2*[cat]
β β β β ββchromeββ¬βchromeββ¬β2*[chromeββ¬β{Chrome_ChildIOT}]
β β β β β β β ββ5*[{CompositorTileW}]]
β β β β β β β ββ{Compositor}]
β β β β β β β ββ{GpuMemoryThread}]
β β β β β β β ββ{MemoryInfra}]
β β β β β β β ββ{Renderer::FILE}]
β β β β β β β ββ{TaskSchedulerRe}]
β β β β β β β ββ{TaskSchedulerSe}]
β β β β β β ββ7*[chromeββ¬β{Chrome_ChildIOT}]
β β β β β β β ββ5*[{CompositorTileW}]]
β β β β β β β ββ{Compositor}]
β β β β β β β ββ{GpuMemoryThread}]
β β β β β β β ββ{MemoryInfra}]
β β β β β β β ββ{Renderer::FILE}]
β β β β β β β ββ{ScriptStreamerT}]
β β β β β β β ββ{TaskSchedulerRe}]
β β β β β β β ββ{TaskSchedulerSe}]
β β β β β β ββchromeββ¬β{Chrome_ChildIOT}
β β β β β β β ββ5*[{CompositorTileW}]
β β β β β β β ββ{Compositor}
β β β β β β β ββ{GpuMemoryThread}
β β β β β β β ββ{Media}
β β β β β β β ββ{MemoryInfra}
β β β β β β β ββ{Renderer::FILE}
β β β β β β β ββ{ScriptStreamerT}
β β β β β β β ββ{TaskSchedulerRe}
β β β β β β β ββ{TaskSchedulerSe}
β β β β β β ββ2*[chromeββ¬β{Chrome_ChildIOT}]
β β β β β β ββ5*[{CompositorTileW}]]
β β β β β β ββ{Compositor}]
β β β β β β ββ{GpuMemoryThread}]
β β β β β β ββ{Renderer::FILE}]
β β β β β β ββ{ScriptStreamerT}]
β β β β β β ββ{TaskSchedulerRe}]
β β β β β β ββ{TaskSchedulerSe}]
β β β β β ββnacl_helper
β β β β ββchromeββ¬βchrome
β β β β β ββ{Chrome_ChildIOT}
β β β β β ββ{MemoryInfra}
β β β β β ββ{TaskSchedulerSe}
β β β β β ββ{Watchdog}
β β β β ββ{AudioThread}
β β β β ββ{BrowserWatchdog}
β β β β ββ{Chrome_CacheThr}
β β β β ββ{Chrome_DBThread}
β β β β ββ{Chrome_FileThre}
β β β β ββ{Chrome_FileUser}
β β β β ββ{Chrome_HistoryT}
β β β β ββ{Chrome_IOThread}
β β β β ββ{Chrome_ProcessL}
β β β β ββ{Chrome_SyncThre}
β β β β ββ{CompositorTileW}
β β β β ββ{CrShutdownDetec}
β β β β ββ{D-Bus thread}
β β β β ββ{Geolocation}
β β β β ββ{IndexedDB}
β β β β ββ{LevelDBEnv}
β β β β ββ{MemoryInfra}
β β β β ββ{NetworkChangeNo}
β β β β ββ{Networking Priv}
β β β β ββ4*[{TaskSchedulerBa}]
β β β β ββ6*[{TaskSchedulerFo}]
β β β β ββ{TaskSchedulerSe}
β β β β ββ{WorkerPool/3166}
β β β β ββ{WorkerPool/5824}
β β β β ββ{WorkerPool/5898}
β β β β ββ{WorkerPool/6601}
β β β β ββ{WorkerPool/6603}
β β β β ββ{WorkerPool/7313}
β β β β ββ{chrome}
β β β β ββ{dconf worker}
β β β β ββ{extension_crash}
β β β β ββ{gdbus}
β β β β ββ{gmain}
β β β β ββ{gpu-process_cra}
β β β β ββ{inotify_reader}
β β β β ββ{renderer_crash_}
β β β β ββ{sandbox_ipc_thr}
β β β β ββ{threaded-ml}
β β β ββcompizββ¬β{dconf worker}
β β β β ββ{gdbus}
β β β β ββ{gmain}
β β β β ββ8*[{pool}]
β β β ββconkyβββ6*[{conky}]
β β β ββ2*[dbus-daemon]
( .... many lines deleted to fit in 30k limit .... )
ββvnstatd
ββwhoopsieββ¬β{gdbus}
β ββ{gmain}
ββwpa_supplicant
As you can see a typical Ubuntu login contains many many PIDs (Process IDβs).
We can narrow it down to our running script using:
$ pstree -g -p | grep display-auto
|-cron(1198,1198)---cron(1257,1198)---sh(1308,1308)---display-auto-br(1321,1308)---sleep(26552,1308)
We see:
cron
started a shell (process ID 1308 and session ID 1308)- The shell calls our program running under process ID 1321 and session ID 1308 (matching the shell)
- Our program calls
sleep
under process ID 26552 and again session ID 1308
At this point we can use pkill -s 1308
and it would kill the entire session which includes the shell, our program display-auto-brightness
and the sleep
command. Instead we will use kill 26552
to only kill the sleep command forcing our program to wake up and adjust the brightness.
Typing this manually in the terminal you see:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@dell:~$ pstree -g -p | grep display-auto
|-cron(1198,1198)---cron(1257,1198)---sh(1308,1308)---display-auto-br(1321,1308)---sleep(32362,1308)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@dell:~$ sudo kill 32362
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@dell:~$ pstree -g -p | grep display-auto
|-cron(1198,1198)---cron(1257,1198)---sh(1308,1308)---display-auto-br(1321,1308)---sleep(1279,1308)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@dell:~$ sudo kill 1279
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@dell:~$ pstree -g -p | grep display-auto
|-cron(1198,1198)---cron(1257,1198)---sh(1308,1308)---display-auto-br(1321,1308)---sleep(4440,1308)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@dell:~$
The next step is to do it when the laptop wakes up from suspend. For example when lid closed it was full dark and screen brightness was set at β300β. When lid is opened it is daylight and brightness needs to be set to β2000β. Of course the program would wake up on itβs own in 1 to 59 seconds but itβs more comfortable for the brightness to be set instantly.
Iβll post the suspend/resume code after itβs written. Hopefully this weekend.