The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 25,749β€…    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: April 8, 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:

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.

⇧ How do I stress test CPU and RAM (at the same time)? Brand new desktop randomly powers off  β‡©