Marius van Witzenburg "Learned my lesson in life, now setting my action to stay in life."

16Mar/100

Getting SSH to execute a command/process in the background on target machine

If you try to accomplish this directly with SSH... I can tell you, this can be hard to get the job done!

But the solution is quite simple... Create a wrapper around your command which does the job for you.

First prepare your SSH command like:

ssh -ax marius@192.168.3.7 "sh /home/marius/ssh-wrapper > /dev/null 2>&1; exit"

Second is to create the wrapper:

#!/bin/sh
sh /home/marius/run &

You don't have to make the files executable since you prefix it with sh.

This works for me on 2 machines without any problems so far.
Good luck! :-)

2Mar/100

Shellproxy, an easy tool to run shell commands from PHP or other languages

This is a simple script that runs a shell command from PHP with the function system().
It sometimes happens that a command doesn't close the stdin or stdout, resulting the system() function to halt and wait for it to finish...
This causes PHP to reach the 30 second time limit and kill the script.

This script runs it in the background and you could make your commands send their output to a log file and still having full control.

Read the code below how to use it.

You can compile it by placing the code into 'shellproxy.c' and then compile it with the following command:

gcc -o shellproxy shellproxy.c

I hope this is something useful for you as it was for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio .h>
#include <stdlib .h>
#include <string .h>
 
int main(int argc, char *argv[]) {
    char command[1024];
    int t;
 
    if (argc == 1) {
        printf("\nShell Proxy\n");
        printf("By Marius van Witzenburg <http ://mariusvw.com>\n\n");
        printf("You will mostly use this program from PHP or any other\n");
        printf("webscripting language to background run programs\n\n");
        printf("Usage:\n");
        printf("- CLI:\n");
        printf("shellproxy \"<cmd> <arg>\"\n");
        printf("shellproxy \"<cmd> <arg> > redirect.txt\"\n");
        printf("- PHP:\n");
        printf("system('shellproxy \"<cmd> <arg> &\"');\n");
        printf("system('shellproxy \"<cmd> <arg> > redirect.txt\"');\n\n");
    }
 
    command[0] = 0;
 
    for (t = 1; t < argc; t++) {
        strcat(command, argv[t]);
        strcat(command, " ");
    }
 
    fclose(stdin);
    fclose(stdout);
    fclose(stderr);
 
    system(command);
}

It would be nice if you keep my name mentioned into this code if you use it.

Ps. Donations are welcome... ;-)

25Oct/090

Make bash act like CSH’s history when you press up or down.

The configuration on FreeBSD is quite simple.

You can add the code below to the following files:

/etc/inputrc
~/.inputrc

The code:

"\e[A":history-search-backward
"\e[B":history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on

This can be really useful if you use long commands quite fast after each other and just have to change a tiny piece, for example a username.

This has been tested on FreeBSD 6/7/8 with the latest Bash version from the FreeBSD ports.