Skip to content

Instantly share code, notes, and snippets.

@mcuyar
Created July 16, 2014 15:32
Show Gist options
  • Save mcuyar/a8e7dcaab1e1a4630e27 to your computer and use it in GitHub Desktop.
Save mcuyar/a8e7dcaab1e1a4630e27 to your computer and use it in GitHub Desktop.
Send messages to logged in terminal user from your laravel application
<?php
if(! function_exists('tlog')) {
function tlog($logged, $message = '', $user = 'vagrant')
{
// Only available in test environments
if (app()->environment() === 'production') {
return;
}
$log = 'echo "\n';
if($message !== '') {
$log .= 'Message: ' . (string) $message . '\n\n';
}
$log .= 'Start------------------------------ \n\n';
if(is_string($logged) || is_int($logged)) {
$log .= (string) $logged;
}
else if(is_array($logged)) {
$log .= print_r($logged, true);
}
else if(is_object($logged)) {
if(method_exists($logged, 'toArray')) {
$log .= print_r($logged->toArray(), true);
}
else {
try {
// Use var export to catch bad objects
$log .= var_export($logged, true);
}
catch(Exception $e) {
$log .= $e->getMessage();
}
}
}
$log .= '\n\nEnd------------------------------ \n';
$log .= '" | write '. tlog_find_user($user);
exec($log);
}
}
if(! function_exists('tlog_find_user')) {
function tlog_find_user($user)
{
$exec = shell_exec('who -us');
$array = explode(' ', $exec);
$array = array_filter($array);
$array = array_chunk ($array , 6);
$users = [];
foreach($array as $item) {
if(count($item) >= 2 && !isset($users[$item[0]])) {
$users[$item[0]] = $item[0].' '.$item[1];
}
}
if(isset($users[$user])) {
return $users[$user];
}
return false;
}
}
@stevenwadejr
Copy link

You can send colors through if you send the output via > /dev/ttysXXX.

Example

echo "\e[31mAlert\e[0m" > /dev/ttys000 sends a message of "Alert" colored in red text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment