Linux command on wake up from hibernate/suspend - linux

I'm new to Linux and running Mint. I've seen a lot of documentation on creating commands that run when the os is started up from the computer being powered off. Is there a way to make similar commands to run when the os wakes up from hibernate or suspend? (For context, I'm running 'rfkill block bluetooth' on startup and would like to when my pc wakes up from hibernate as well).

place your commands in a script file and ensure you have sufficient owner/permissions to execute in /lib/systemd/system-sleep/ so once your OS suspends from sleep, its going to execute.
For more information
man systemd-sleep
https://askubuntu.com/questions/226278/run-script-on-wakeup

Related

How exactly a 'shutdown -h' "HALT" differ from "shutdown" in linux

Suppose I have 20 process/deqamons running in my linux system,
How different the HALT will have an effect on my process/deamons, when compared to a SHUTDOWN
Generally, one uses the shutdown command. It allows a time delay and warning message before shutdown or reboot, which is important for system administration of multiuser shell servers; it can provide the users with advance notice of the downtime.
As such, the shutdown command has to be used like this to halt/switch off the computer immediately (on Linux and FreeBSD at least):
shutdown -h now
Or to reboot it with a custom, 30 minute advance warning:
shutdown -r +30 "Planned software upgrades"
After the delay, shutdown tells init to change to runlevel 0 (halt) or 6 (reboot). (Note that omitting -h or -r will cause the system to go into single-user mode (runlevel 1), which kills most system processes but does not actually halt the system; it still allows the administrator to remain logged in as root.)
Once system processes have been killed and filesystems have been unmounted, the system halts/powers off or reboots automatically. This is done using the halt or reboot command, which syncs changes to disks and then performs the actual halt/power off or reboot.
On Linux, if halt or reboot is run when the system has not already started the shutdown process, it will invoke the shutdown command automatically rather than directly performing its intended action. However, on systems such as FreeBSD, these commands first log the action in wtmp and then will immediately perform the halt/reboot themselves, without first killing processes or unmounting filesystems.
On POSIX systems the shutdown command switches runlevels, and executes the appropriate scripts.
On FreeBSD the "halt" command is an ACPI thing...
If you have particular concerns or would like to know things the general documentation wouldn't readily address, please feel free to refine your query.

Killing a process windows CE

I need to implement a program that will run on Windows CE 6.0, in C++, and that aims to kill a specific process at a specific moment.
I have tried to use 'TerminateProcess(ProcessHandle, ExitCode)'. It works, and kills the process whose handle I passed. However, when killing the specific process I want to kill, the machine stops responding, which is not acceptable.
I can kill the process via tellnet, if I use:
telnet 10.120.12.5 (IP of the Windows CE machine)
shell -d
kp 6522589 (example ID of the proccess)
That works. It kills the process and the machine does not stop running.
So the question is, how can I insert this command into my code? or, how can I call a script which does this from my code? (the script will have to be stored inside the windows CE machine).
Any other suggestion will be welcome.
How do you get the ProcessHandle? You usually need to free it with CloseHandle() after you are done with it (after calling TerminateProcess()).
You can use CreateProcess() to run another program, e.g. the shell/kp program.

What happens first when you login or logout from the system in Linux

I was asked this question from my friend that
when you login our logout from a Linux based system then what all
process run and in which sequence?
Being a noob in Linux and not into real system admin kind of job, so it was difficult for me to answer. Is there any particular tutorial on such concepts or can somebody explain what exactly happens while logging in or logging out from the Linux System?
Assume it is console terminal not GUI.
In Linux, the flow of control during a boot is from BIOS, to boot loader, to kernel. The kernel then starts the scheduler (to allow multi-tasking) and runs the first userland (i.e. outside kernel space) program Init (which is mostly responsible to run startup scripts for each runlevel), at which point the kernel goes idle unless called externally.
init (short for initialization) is a program for Unix-based computer operating systems that spawns all other processes. It runs as a daemon and typically has PID 1. The boot loader starts the kernel and the kernel starts init. If one were to delete init without a replacement, the system would encounter a kernel panic on the next reboot.
When init starts, it reads a file called inittab, usually located in /etc. This file tells init which programs should be run under which conditions. Not only does init run the startup scripts that bring the rest of the system up, but init also takes care of shutting the system down. Commonly, init will start a program called "getty" to spawn a new terminal (or tty), and "login" for login prompt. This configuration is set inside inittab. In recent version of linux, inittab is replaced with /etc/init/*.conf and scripts inside /etc/init.d/
On shutdown, Init is called to close down all user space functionality in a controlled manner, again via scripted directions, following which Init terminates and the Kernel executes its own shutdown.
See Linux startup process Wiki page
Login: Getty is the process which will take care of complete login process.
Init creates the getty process
getty process initiates login command
login command try to check user credentials
getty creates user shell process
getty read shell property files
getty provides you with PS1 prompt
On logout, the shell program exits and we return to step 1.
See login process at this website 1
See login process at this website 2

What can cause SIGHUP to be generated?

We have about 40 computers running identical hardware and software. They all run Ubuntu 11.10. They all have just one user account to log in. The .profile file is set up to launch a daemon process. The code for the daemon is written in C.
Once in a few weeks, we get a report that the daemon is no longer running. This does not happen on all computers but just one or two. We cannot reproduce the problem consistently.
Looking at the code, the application quits when it receives either SIGHUP or SIGTERM.
As I understand, SIGHUP is generated when a user logs off. In our case, the user never logs off. I am wondering if it is possible that SIGHUP could have been generated for some other reason. Any other thought would be appreciated.
Well, there are a couple of things to note about SIGHUP. Firstly, its origin is from the concept of a hang-up, i.e. loss of connection to a console over something like a modem. In modern parlance this generally means it has lost its controlling tty. Unless you've taken care to detach from your tty, any program started in a given terminal will receive a SIGHUP when the terminal is closed. See here for details on how to do this in your program. Other options include:
running your program inside screen or tmux
run your program with nohup or some other daemonising framework
The other possibility is something is deliberately sending your process a SIGHUP which by "tradition" is often used to signal a process that it should re-read its configuration.
Signals can be sent using kill utility or kill syscall.
Of course, you can try and find out who is sending that signal or disconnecting your terminals or network connections, but there is simpler practical way to fix your problem.
When code is supposed to run as a daemon, but really isn't (just like yours), there is a wrapper that can turn any program into daemon. Surprise - this wrapper is called daemon! It has lots of options, probably most importantly for you, option to automatically restart your utility should it ever die for any reason.
If this command is not installed on your Ubuntu, just sudo apt-get install daemon, and man daemon to get started.

Watchdog for Linux

Are there any watchdog tools or libraries on Linux for the following purpose? I would like to build a watchdog executable which starts 2 processes and restarts them if:
processes crash
processes become unresponsive (e.g. hang for some reason)
Internet search found watchdog.c but I am not sure if that can be used for my purposes, it looks pretty low level.
I could run my processes as init programs (daemons) as suggested here, but I am not sure if Linux would then recognize that the process is hanging (e.g. due to a deadlock)
We use monit here: http://mmonit.com/monit/ it will let you do the restart thing it is also highly customizable regarding how to check and how to react via scripts

Resources