How can I send a SIGUSR1 signal in code to attach to process? - node.js

In VScode, I can choose nodeJS process that was ran with '--inspect' flag and attach to it as shown on the screenshot below:
VSCode Sigusr1
But I have to do it manually everytime. Is it possible to execute the SIGUSR1 signal already in the code so VSCode would do it for me upon running my code?

Related

How to run process which only attach stdin, stdout and stderr to another running process

I have application which spawns child process ans used stdin/stdout/stderr to communicate with it. I can modify a spawn command.
What I would like to achieve is run this child process manually before spawn from application (eg because I want to run in debugger) and attach to it with standard spawn.
Is it possible?
I know there is /proc/PID/fd/0 and so on. But I still don't know if it's possible to connect it by spawning another "mock process" to it.
Attach as a debugger to the child process. Create new pipes for stdin, stdout, and stderr, and use dup2 to put them in place. If you're on Linux 5.6 or newer, use pidfd_getfd to get the other ends of these pipes into your other process. Otherwise, send them as SCM_RIGHTS ancillary data over a Unix domain socket to your other process.

reboot handling in linux

I'm trying to flush some data on service stop.
Based on documentation SIGINT is sent before killing the services, but looks like linux reboot command works another way. Maybe it works in force mode because of runlevel, but it doesn't invoke SIGINT, it kills all applications without any notice.
Is there any way I can handle reboot or change this behavior?
The problem you have is that reboot sends SIGTERM and not SIGINT.
Change your signal handler to handle SIGTERM and you'll be fine.

Qt DBUS signals are delayed when process is not started as user

I am having a strange problem trying to get a small program running at boottime before any user has logged in.
I have used different methods of running the program at boot and nothing has helped me yet! Anyway, I have a small Qt program that communicates with a different program via DBUS (systembus). Currently the program is started with upstart and registers the service etc. fine on the systembus. I can also call different methods of the program using "qdbus --system", these methods return immediately with correct return values.
I have set the Qt program to send a DBUS signal every second. when monitoring this with dbus-monitor i only see a signal being passed every 2-5 seconds but when I kill the program and log in as a user (root) the program sends its signals just fine (every second).
How is this possible? I want the program sending the signals to run when the computer boots and have another user application connected to the signals to take action when the signal is received.
I have tried to run the program as an upstart job, using the init.d way and the normal rc.local way but all with the same result.
Do I have to have a user logged in for this to work?
Thanks,
Sisco

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.

standard way to communicate with a running process via shell script in linux

Is there a standard linux/unix pattern for communicating with long running process?
For example, I have few hundred process, written in c++, and running on various machines and I would like to send them a command like reload configuration, start, stop etc via shell scripts.
Signals.
If you're trying to trigger simple actions like the start/stop/reload configuration as you've described, the most common method is to use signals.
From your shell script you can use the kill command to send a specific signal to a specific process.
Within your process you would implement one or more signal handlers. The signal handler(s) are registered to receive one or more signals by using the signal() function, or the sigaction() function.
Conventionally SIGHUP is used to trigger a reload of configuration. SIGSTOP and SIGCONT may be appropriate for pausing and resuming.
man 7 signal will show you a complete list of available signals to choose from.
If you need to trigger more complex actions you can create a named pipe. Have your process create the pipe and, from your shell script, just echo commands to it.
Since you also care about remote processes, and assuming you can modify the source code of all your programs, you could consider some way to communicate with them:
defining your own small textual protocol, and have each process listening on some socket or some named pipe. You probably would need some multiplexing syscall like poll
use existing libraries and tools like MPI, Corba, or perhaps D-Bus or ONC/RPC/XDR
change the configuration files of your application, and have signal conventions, e.g. catch SIGHUP to reload the configuration, and SIGTERM to properly terminate it (but there is no way to send a signal remotely; you'll need e.g. to ssh some kill command).

Resources