Eu nunca encontrei uma maneira de realmente desembaraçar com um shell, existem muitas razões pelas quais algo permanecerá conectado. Eu escrevi um programa C muito pequeno para separar todos os comandos que você executa depois dele. Espero que isso ajude você.
----- daemonize.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
int main(int argc, char *argv[]) {
int i;
// int reterr;
pid_t pid, sid;
//Fork the Parent Process
pid = fork();
if (pid < 0) { exit(EXIT_FAILURE); }
//We got a good pid, Close the Parent Process
if (pid > 0) { exit(EXIT_SUCCESS); }
//Change File Mask
umask(0);
//Create a new Signature Id for our child
sid = setsid();
if (sid < 0) { exit(EXIT_FAILURE); }
//Change Directory
//If we cant find the directory we exit with failure.
if ((chdir("/")) < 0) { exit(EXIT_FAILURE); }
//Close Standard File Descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
//----------------
//Main Process
//----------------
for(i=0; i < argc - 1; i++) {
argv[i]=argv[i+1];
}
argv[argc-1] = 'CXX = gcc
# Implicit rules needed to build .o files and Metaobject stuff (_m.o)
.SUFFIXES: .c .o .h
.c.o:
$(CXX) -c $(XTRA_INCL) $< -o $@
OBJECTS = daemonize.o
daemonize: $(OBJECTS)
$(CXX) -o $@ -pipe -O2 $(OBJECTS) $(RPATH_XT)
strip --strip-unneeded $@
clean:
@rm -f core *.o *_m.* *.bak *.elf
';
execv(argv[0], argv);
//reterr = execv(argv[0], argv);
//printf("execv failed with '%s'\n", strerror(errno));
//Close the log
closelog ();
}
--- Makefile
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
int main(int argc, char *argv[]) {
int i;
// int reterr;
pid_t pid, sid;
//Fork the Parent Process
pid = fork();
if (pid < 0) { exit(EXIT_FAILURE); }
//We got a good pid, Close the Parent Process
if (pid > 0) { exit(EXIT_SUCCESS); }
//Change File Mask
umask(0);
//Create a new Signature Id for our child
sid = setsid();
if (sid < 0) { exit(EXIT_FAILURE); }
//Change Directory
//If we cant find the directory we exit with failure.
if ((chdir("/")) < 0) { exit(EXIT_FAILURE); }
//Close Standard File Descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
//----------------
//Main Process
//----------------
for(i=0; i < argc - 1; i++) {
argv[i]=argv[i+1];
}
argv[argc-1] = 'CXX = gcc
# Implicit rules needed to build .o files and Metaobject stuff (_m.o)
.SUFFIXES: .c .o .h
.c.o:
$(CXX) -c $(XTRA_INCL) $< -o $@
OBJECTS = daemonize.o
daemonize: $(OBJECTS)
$(CXX) -o $@ -pipe -O2 $(OBJECTS) $(RPATH_XT)
strip --strip-unneeded $@
clean:
@rm -f core *.o *_m.* *.bak *.elf
';
execv(argv[0], argv);
//reterr = execv(argv[0], argv);
//printf("execv failed with '%s'\n", strerror(errno));
//Close the log
closelog ();
}