I have a problem when run MySQL restore dump file with Swift Process.
let command = "/usr/local/bin/mysql -h theHost -P 3306 -u root -pTheInlinePassword example_database < dumpFile.sql"
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = command.components(separatedBy: " ")
task.launch()
The problem is Process doesn't understand standard input <
. How I can run command with standard input like this. Thanks.
Update:
let task = Process()
task.launchPath = "/usr/local/bin/mysql"
task.arguments = ["-h", "theHost", "-P", "3306", "-u", "root", "-pTheInLinePassword", "example_data"]
task.standardInput = try! FileHandle(forReadingFrom: filePath!)
task.launch()
I tried code bellow. This works for me
The
< filename
syntax is a feature provided by the shell, not something that programs themselves ever deal with.The proper way to handle this is to construct a
FileHandle
for reading fromdumpFile.sql
and then set thatFileHandle
as thestandardInput
property of theProcess
.As a side note, I don't know why you're using
/usr/bin/env
as your launch path, since you're not relying on PATH lookup or setting any environment variables.I had been trying to get this to work for some time using Process and have been unsuccessful. What did work, however, is diving into some of the Darwin framework and using posix_spawn.
This script will spawn a subprocess and hand control over to it until the process terminates (much like the os.system call in Python).
I am sure this script could be improved, but might get you closer to where you want to go.
Hope this helps!