Enter, Ctrl-a and d generate normal ASCII codes.
So a possible solution could be a program that creates an unnamed pipe (pipe()
), then fork()
s a child process which first binds the read end of the pipe to its standard input, then executing screen
in the child process (execve()
or similar). If that program is started, you can write the command lines required to start a process to the write end of the created pipe.
How you put the tasks into that program is another topic. You could, for example, write a small scheduler yourself (something like a queue of jobs and some code that starts at most N processes in parallel).
== EDIT ==
For Linux (or maybe some UN*Xes, too), the program could look like the following:
#include <sys/types.h>
#include <sys/linux.h>
#include <unistd.h>
int main(void) {
int fds[2] = {0};
pid_t pid = 0;
pipe(fds); /* Creates an unnamed pipe */
pid = fork(); /* Forks a new process */
if (pid == 0) {
static char const *argv[] = {"/usr/bin/screen", NULL};
/* Note: The array might need to be changed,
* depending on your requirements
* (eg. command-line arguments)
*/
dup2(fds[0], stdin); /* Binds the read end of the pipe to stdin */
execve(argv[0], argv, NULL);
/* If you reach this point, your execve() failed */
}
sleep(1); /* Waits for the child process to execute
* screen */
char const data[] = "./MyJob.sh\n\x00d";
/* Note: You must replace the '\x00' by the
* ASCII code of C-a!
*/
write(fds[1], data, sizeof(data));
/* Writes the name of the job along with the
* control codes to the child process
*/
int retcode = 0;
waitpid(pid, &retcode, 0);
/* Waits for the child process to terminate */
/* Note: WEXITSTATUS(retcode) gets the exit
* status of the child process */
return 0;
}
This program shall illustrate the idea, it lacks the necessary error handling.
me@pc:~/code> screen -md ./myJob.sh Must run suid root for multiuser support.
– Sibbs Gambling Oct 08 '14 at 12:55~/.screenrc
, and in your systemscreenrc
(typically/etc/screenrc
or/etc/screen/screenrc
)? – Gilles 'SO- stop being evil' Oct 08 '14 at 15:16~/.screenrc
, but I do have/etc/screenrc
. It says `# this is the global screenrc file. Handle with care.termcapinfo xterm* G0:is=\E[?4l\E>:ti@:te@ termcapinfo linux me=\E[m:AX` Oh, btw, I am doing this on a HPC server.
– Sibbs Gambling Oct 08 '14 at 15:21screen -md ./myJob.sh
, right? There mustn't be a/
in the session name. I've fixed my script to use only the base name of the executable as the session name and not the full path. – Gilles 'SO- stop being evil' Oct 08 '14 at 16:24