Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell scripting and jQuery

Tags:

jquery

ajax

shell

I want to execute a linux command that returns the currently logged on user in linux.

Using jQuery and ajax, passing this command to the PHP, which will receive and execute this command using exec() function. The value should be stored and returned.

Upon returning, jquery ajax should return it to a contents of a span tag.

How can this be achieved?

like image 374
user478636 Avatar asked Apr 18 '11 15:04

user478636


2 Answers

Use .load(). Something like this:

JavaScript

$('#my-span-id').load('/path/to/page.php');

PHP

<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>
like image 92
Matt Ball Avatar answered Oct 13 '22 19:10

Matt Ball


This javascript sample run a tail command using node.js

#!/usr/bin/env node

var readline = require('readline');
var cp = require('child_process');
var tail = cp.spawn('tail', ['-500', 'mylogfile.log']);
var lineReader = readline.createInterface(tail.stdout, tail.stdin);

lineReader.on('line', function(line) {
    console.log('Line: ' + line);
});

tail.on('close', function(code, signal) {
    console.log('ls finished...');
});

Reference Tutorial

like image 39
Juanma Avatar answered Oct 13 '22 18:10

Juanma