Re: Detach running program from terminal

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rodolfo J. Paiz wrote:

At 20:38 10/22/2003, you wrote:

I ma talking about a GUI program, like if I launch synaptic from the
terminal, but want to detach it. screen is useful if i want something to
run in the background.


I don't think there is a general solution to that. In synaptic's case specifically, you can use the command-line equivalent apt-get. But for other programs generally, all I can think of is to run them inside a VNC session. Not much else comes to mind.

Do you want the display to be detached as well? If not then you might find the attached program (no pun intended) useful. It's a bit like nohup, but it completely detaches the program from the invoking terminal. For example, "detach synaptic". It's only small -- I hope it doesn't invoke the wrath of the list manager.


jch

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>

int main (int argc, char *argv[])
{
    int pid, fd, p[2], i;

    if (pipe (p) < 0) {
	perror ("pipe");
	exit (1);
    }
    if ((pid = fork()) < 0) {
	perror ("fork");
	exit (1);
    }
    if (pid != 0)
    {
	close (p[1]);
	if (read (p[0], &i, sizeof (int)) == sizeof (int))
	{
	    char *err = strerror (i);
#ifdef __GNUC__
	    struct iovec iov[] = {
		{ argv[1], strlen (argv[1]) },
		{ ": ", 2 },
		{ err, strlen (err) },
		{ "\n", 1 }
	    };
#else
	    struct iovec iov[] = {
		{ NULL, 0 },
		{ ": ", 2 },
		{ NULL, 0 },
		{ "\n", 1 }
	    };
	    iov[0].iov_base = argv[1]; iov[0].iov_len = strlen (argv[1]);
	    iov[2].iov_base = err;     iov[2].iov_len = strlen (err);
#endif
	    writev (2, iov, 4); 
	    exit (1);
	}	    
	exit (0);
    }

    fd = open ("/dev/null", 2);
    dup2 (fd, 0);
    dup2 (fd, 1);
    dup2 (fd, 2);
    for (i = sysconf (_SC_OPEN_MAX); i >= 3; i--)
	(void) fcntl(i, F_SETFD, 1);
//	(void) fcntl(p[1], F_SETFD, 1);
    setsid ();
    signal (SIGCHLD, SIG_DFL);
    argv++;
    execvp (*argv, argv);
    i = errno; /* errno could be a macro, for example */
    write (p[1], &i, sizeof (int));
    exit (1);
}

[Index of Archives]     [Fedora Users]     [Centos Users]     [Kernel Development]     [Red Hat Install]     [Red Hat Watch]     [Red Hat Development]     [Red Hat Phoebe Beta]     [Yosemite Forum]     [Fedora Discussion]     [Gimp]     [Stuff]     [Yosemite News]

  Powered by Linux