PHP code example of tourze / server-shell-bundle

1. Go to this page and download the library: Download tourze/server-shell-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / server-shell-bundle example snippets


return [
    // ... other bundles
    ServerShellBundle\ServerShellBundle::class => ['all' => true],
];



use ServerShellBundle\Service\ShellScriptService;
use ServerNodeBundle\Entity\Node;

// Inject the service
public function __construct(
    private ShellScriptService $shellScriptService
) {}

// Create a new script
$script = $this->shellScriptService->createScript(
    name: 'System Update',
    content: "#!/bin/bash\napt update && apt upgrade -y",
    workingDirectory: '/tmp',
    useSudo: true,
    timeout: 300,
    tags: ['system', 'update'],
    description: 'Updates system packages'
);

// Execute script on a node
$node = $this->nodeRepository->find(1);
$execution = $this->shellScriptService->executeScript($script, $node);

// Check execution status
echo "Status: " . $execution->getStatus()->value;
echo "Result: " . $execution->getResult();

// Schedule script for async execution
$execution = $this->shellScriptService->scheduleScript($script, $node);

// The script will be executed asynchronously via Symfony Messenger
// Check status later using the execution ID
$execution = $this->shellScriptService->findExecutionById($execution->getId());

// Create script
createScript(string $name, string $content, ...): ShellScript

// Update script
updateScript(ShellScript $script, ...): ShellScript

// Execute script
executeScript(ShellScript $script, Node $node): ScriptExecution

// Schedule async execution
scheduleScript(ShellScript $script, Node $node): ScriptExecution

// Find scripts
findScriptById(int $id): ?ShellScript
findAllEnabledScripts(): array
findScriptsByTags(array $tags): array

// Find executions
findExecutionById(int $id): ?ScriptExecution
findExecutionsByNode(Node $node): array
findExecutionsByScript(ShellScript $script): array

// Execute multiple scripts on multiple nodes
$scripts = $this->shellScriptService->findScriptsByTags(['deployment']);
$nodes = $this->nodeRepository->findByEnvironment('production');

foreach ($scripts as $script) {
    foreach ($nodes as $node) {
        $this->shellScriptService->scheduleScript($script, $node);
    }
}

// Create script from template
$script = $this->shellScriptService->createScript(
    name: 'Database Backup',
    content: $this->renderTemplate('database_backup.sh.twig', [
        'database' => 'app_prod',
        'backup_path' => '/backups'
    ])
);

// Monitor execution progress
$execution = $this->shellScriptService->findExecutionById($id);

if ($execution->getStatus() === CommandStatus::RUNNING) {
    echo "Execution time: " . $execution->getExecutionTime() . "s";
} elseif ($execution->getStatus() === CommandStatus::COMPLETED) {
    echo "Completed in: " . $execution->getExecutionTime() . "s";
    echo "Exit code: " . $execution->getExitCode();
}
bash
php bin/console doctrine:migrations:migrate