Waking up from hibernate stopped watch script [LITE]

I made a bash script which is run automatically for every 1 secs using watch command on terminal. Upon waking up the machine from hibernation, the watch command seems to freeze on the last time I close my laptop’s lid, not all the time, but most of the time. Somethings wrong? like for example, some daemon are not running upon waking up or somewhat like that?

Example:
I created a bash script called it “time.sh”, inside we have:

#!/bin/bash
echo $(date)
exit

then execute it using this command:
watch -ctn 1 bash time.sh

it will shows the the date and time, every precisely 1 second, because the script only contain an echo command, so it’s easy to execute. If I put xidel command for webscrapping that would be depends on the speed of data.

How do I know if it’s not running?
I put echo $(date) on the last line of my bash code, so it’s clearly show that the script is still stuck at the closing lid time, not changing when waking up. Sometimes it will run after a minutes or so, sometimes it just freeze.

How do I deal with the situation right now?
Manually: CTRL+C, then press UP then ENTER to re-Execute it

Does your bash script include a portion for Resume?
e.g.

case "$1" in
resume)
...

Or is it something you can us pm-utils for, - sleep, hibernate, suspend?
Edit: Documentation is in /usr/share/doc/pm-utils/HOWTO.hooks.gz

Is script placed in /etc/pm/sleep.d
or in
/lib/systemd/system-sleep/
/usr/lib/systemd/system-sleep/

my script does not include resume option, watch command re-execute it again from begining in x secs after finishing the last bash, so, no point putting if-else or case in the script.

I don’t use command for hibernation, I just close the lid, and it will suspend by itself.

My bash script located at /home.

Well, yes…But your computer is suspending, so the script will too. If you want it to resume itself upon waking without you having to restart it, you must use ‘resume.’
You also can script it so that it does not suspend, even if the computer does and does not resume because it did not suspend, if you like.

EDIT: maybe I need to be more clear:
If the script says to execute a function after 1 second and you wake the computer up from suspend, the script will not execute after one second of waking unless the Script Itself is started/ initialized. There must be a function telling it to start, then it can ‘execute command after 1 second.’

Yes, something is stopping the script from running the watch command when resuming the session.

The “resume” you mentioned about, I looked for it and only found the fg and bg command. To use the command I do CTRL+Z then fg if I want to make the script foreground, or bg if I need it to run on background. But that is almost the same as my manual solution, isn’t that?

maybe I should bring this problem to stack forum, since this is more related to scripting. and post the solution here once i got it.

Yes, suspending your computer is stopping the script.
It will not Auto-resume itself because no part of it tells it to do so. It is not being stopped from resuming, because no part of the script tells it to resume in the first place.
Resuming your session does not automatically resume your script for you.

When you suspend the computer by closing the lid, this translates to you saying to the computer, "I would like to suspend the machine, if that is alright with you."
If there are background processes running that it needs to finish, it will continue until finished without suspending. So let's say you close the lid, it kills the Screen and Backlight, but continues to run until those processes are complete. This does not always happen, maybe about ten percent of the time or a bit more or a bit less... It happens when a process still running must finish before closing. This can last a few seconds, a few minutes or if there is a loop, can last several hours.

Once it suspends, then the script is stopped. It cannot resume without being told, either by you starting it or by you including a part in the script that tells itself to resume. I think you are thinking that if the script is to execute a command every second, then it should do so within 1 second. But it cannot do so at all unless the Script is Started.
The script can be started with a Resume function in the script, for example:

#!/bin/sh
case "$1" in
hibernate|suspend)
;;
thaw|resume)
vlock -ans
;;
*) exit $NA
;;
esac

And the script placed in your /lib/systemd/system-sleep/ folder
I pointed to the above documentation on your computer.
Stack is a good choice, too.

Other references that may help:
https://www.addictivetips.com/ubuntu-linux-tips/run-scripts-and-commands-on-suspend-and-resume-on-linux/

@Aravisian say bro, my question is crystal clear right? is there something wrong with my question?

  1. I run watch bash bash.sh
  2. I close laptop lip to suspend
  3. next day, I open laptop lid to wake up
  4. BOOM! 2 possibilities (50/50 chance):
    4a. the watch command continue running.
    4b. Why is it not running?

it this too hard to digest bro? what I dont like from stack community is that 90% of the population are nerd that has some disgusting hobby, dowvoting my question. I only found 1 or 2 salutable expert of all stack universe of all time.

Quenyan,

Please re-read each and every post I have made in this thread carefully.

It makes sense if you think about it… If a Script automatically started itself, then every script in every piece of software on your computer would start the moment you opened the lid or booted the computer.

Clearly, that is not how it works.

Before a command within a script can be issued, the Script Itself Must Be Started (or resumed).

I explained how this works three times above and this makes a forth time.

I explained once about how a program can be still running sometimes even after a suspend was placed on the machine and how sometimes it does not.

I have answered your question clearly and even provided some Script Material that you can use and I provided resources.

So… Let me make Myself Crystal Clear for you: When people like me take a lot of time, for free, to help people out without much in the way of thanks… You would be Wise To Speak to us Courteously.

@Aravisian Sorry if you felt like I’m mad at you, I am not. I’m just mad at Stack community, and I just sharing my thoughts here with you.

And, about your saying “Once it suspends, then the script is stopped. It cannot resume without being told, either by you starting it or by you including a part in the script that tells itself to resume.”, I couldn’t agree with that because, how do you explain that it sometimes resumed by itself without being told. ? I didn’t say that it is not resuming all the time, just sometimes. 50/50 chances.

Again, I apologized for the ruckus. :pray:t2:

It did not resume itself- it did not stop running and there is an important difference.
Whether you agree or not- is not relevant. Should you disagree with me if I say that the Proton has a positive charge, your disagreement will not change the state of the Proton.
A script on the machine Cannot Start without being Told, Instructed, Authorized to do so.
Once it is running, if it fails to stop... then it failed to stop. But that is not the same thing as running, stopping, then starting itself without being instructed to do so.

If you want your script to resume from suspend /Hibernate, every time you wake the computer 100% of the time, you must instruct it do so with your script.

I certainly understand where this can be confusing. It appears to run some of the time without you having to tell it to... And it says within the script that it is supposed to perform an action every second.
However, as I pointed out - If a script could start itself without authorization or instruction- Then all your scripts could be going haywire the moment that you wake or boot up the computer.
As this is not happening, your only rational conclusion must be that the scripts cannot start themselves without instruction or authorization - Even if sometimes, the script is not stopped when you suspend the computer.

I re-read all things you said above, and think about it a little while. Yes, it’s a bit confusing to me ALOT, but I think I begin to grasp the idea.
So, the script that you gave me above, is that for both pausing and resuming the watch command?
And, I still trying to figuring out how to use the script… :thinking:

No, hibernate is the "pause" so to speak. It merely tells the script to resume issuing the command contained within the script upon wake.

I see, and … what does vlock -ans do in the script?

ah, nevermind, I looked it up, so vlock is for virtual locking. I didn’t installed it so it does not do anything to my system.

Good point, you may not have the session lock installed.
I apologize, I can see how that would confuse you. The script I posted was more an Example than a script specific to you.
I am not sure what your script is for or what it is supposed to do.
The link I posted here:


Would probably give better examples that you can use.
Essentially, you want to make a script as the guide teaches, that references the command you want to start and run upon resume.
The script must be placed in /lib/systemd/system-sleep and /usr/lib/systemd/system-sleep

I am not sure what your script is for or what it is supposed to do.
So on my another laptop, I only open 1 terminal, and executed watch -ctn 1 bash myscript.sh, inside it has xidel command for webscrapping, and it showing the data that is updated regularly. Then lastly it prints the date and time. That’s it, the script ends there. The purpose of that laptop is just that, showing the information I need on the screen.

/lib/systemd/system-sleep and /usr/lib/systemd/system-sleep ,both of them?

Question
So, inside that sleep folder has hdparm script, do I create myscript.sh or I just edit that hdparm.sh?

Now, I am the one who is confused. Earlier, you pasted a snippet of your bash script and I actually thought that was all there was to it.
Which, of course, made it no surprise that it was not working on resume. Or doing much of anything else.

I am not trying to be nosy. I only point out that I do not fully know your intentions so I may not be able to give better pointers.
Either way, everything I said above still applied- You need a script that handles "resume from suspend / hibernate" in order to Start Your Script on resume each time.

Yes, Both of Them.

No, do not edit the hdparm script.

You would create a new script- The tutorial I posted above should help you but Please Read it Carefully because making an error in init can cause you major headaches. This is one of those "do it right the first time" things.

I'm sorry about that, I thought every bash script is simple because it can run in the speed of lightning, unlike heavyweight apps.

No, you're not.

Yes, I will. And.... The tutorial mentioning about if-else pre and post ,while your script is using suspend and resume. Which one is more advance? Or is it the same?

So, today I tried the thing.

  1. I couldn’t find a system-sleep folder in /usr/lib/systemd/ so I made one.
  2. I tried a simple script that does not need printout, such as touch file.txt using the if-else method. but when I try to suspend and wake up, nothing happened.
  3. I named the file to post-suspend.sh if that matters, and chmod +x for both of them.
  4. I tried restarting my laptop, nothing happened.

did I miss a step?